Item_code must be unique

Hi,
I have checked the option “Allow Item to be added multiple times in a transaction”
but still system throw erro message “Item_code must be unique” when entering a purchase invoice using same item code in multiple lines.

Please your help is appreciated.

rgds
Nofal

Hi,

What kind of item is it? Does the item appear in any linked doctypes, a Purchase Order or Purchase Invoice ?

Does the same thing happen with all items?

Hi @smino,
Actually it’s a service item, not stockable.
also I have tested in PO system accept duplicate item code but purchase invoice no?

also in a note I read that stock settings “Allow Item to be added multiple times in a transaction” is generic for all doctypes.

thx

Allow Item to be added multiple times in a transaction is in both Buying Setting and Selling Setting. Try ticking in both documents if you haven’t done it.

Hi @Muzzy,
both checked,

even I have started with totally new purchase invoice it shows same error message with one item line entered.

below trace file

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 325, in db_insert
    ), list(d.values()))
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py", line 156, in sql
    self._cursor.execute(query, values)
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
    result.read()
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, "Duplicate entry '60100000' for key 'item_code_2'")

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/save.py", line 22, in savedocs
        doc.save()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 273, in save
        return self._save(*args, **kwargs)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 296, in _save
        self.insert()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 252, in insert
        d.db_insert()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 339, in db_insert
        self.show_unique_validation_message(e)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 389, in show_unique_validation_message
        raise frappe.UniqueValidationError(self.doctype, self.name, e)
    frappe.exceptions.UniqueValidationError: ('Purchase Invoice Item', '9cf6b0f722', IntegrityError(1062, "Duplicate entry '60100000' for key 'item_code_2'"))
    request.js:355:14
    Traceback (most recent call last):
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 325, in db_insert
        ), list(d.values()))
      File "/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py", line 156, in sql
        self._cursor.execute(query, values)
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
        result = self._query(query)
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
        conn.query(q)
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
        self._affected_rows = self._read_query_result(unbuffered=unbuffered)
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
        result.read()
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
        first_packet = self.connection._read_packet()
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
        packet.check_error()
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
        err.raise_mysql_exception(self._data)
      File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
        raise errorclass(errno, errval)
    pymysql.err.IntegrityError: (1062, "Duplicate entry '60100000' for key 'item_code_2'")

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 64, in application
        response = frappe.api.handle()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/api.py", line 59, in handle
        return frappe.handler.handle()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 24, in handle
        data = execute_cmd(cmd)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 63, in execute_cmd
        return frappe.call(method, **frappe.form_dict)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1054, in call
        return fn(*args, **newargs)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/save.py", line 22, in savedocs
        doc.save()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 273, in save
        return self._save(*args, **kwargs)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 296, in _save
        self.insert()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 252, in insert
        d.db_insert()
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 339, in db_insert
        self.show_unique_validation_message(e)
      File "/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py", line 389, in show_unique_validation_message
        raise frappe.UniqueValidationError(self.doctype, self.name, e)
    frappe.exceptions.UniqueValidationError: ('Purchase Invoice Item', '9cf6b0f722', IntegrityError(1062, "Duplicate entry '60100000' for key 'item_code_2'"))
    request.js:355:14

​

it’s production instance

Installed Apps

ERPNext: v12.10.1 (version-12)

Erpnext Custom: v0.0.1 (master)

Frappe Framework: v12.8.1 (version-12)

I found this property from back end, how I can remove this constraint item_code_2?
image

Tried replicating this in my test instance but could not replicate the error. It allowed to enter the item multiple times. Could you re-check the Buying Settings if the checkbox was checked after creating the Purchase Invoice?

Awaiting your reply.

Hi @michelle,
Thanks for your feedback.
even me I am not able to reproduce it in dev.
actually a constraints called item_code_2 get created since item code is indexed in purchase invoice item code doctype. i have unchecked indexed and still constraints is teheire.

At the end I have deleted constraint from backend then issue resolved.

now from front end index and unique not checked at child doctype level, buying settings “Allow Item to be added multiple times in a transaction” checked.

thx
Nofal