frappe.exceptions.ValidationError: item_code field cannot be set as unique in tabItem, as there are non-unique existing values

Got this error when i ran bench update

frappe.exceptions.ValidationError: item_code field cannot be set as unique in tabItem, as there are non-unique existing values

i tried -

bench --site site1.local console
ps = frappe.get_doc("Property Setter", {"field_name": "item_code", "property": "unique"})
ps.delete()
frappe.db.commit()

Result

DoesNotExistError: Property Setter {u'property': u'unique', u'field_name': u'item_code'} not found
1 Like

Have you set Item Code field as unique in Item DocType ? If yes then uncheck it as you have Item records with same Item Code. Also, do bench build && bench migrate.

2 Likes

I am not able to login to an instance

i m getting the following message
Your system is being updated. Please refresh again after a few moments

Make sure to maintain adequate backup!
Try

bench update --force --reset
bench update --requirements
bench update --patch

I am still getting the following error

File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 768, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 258, in on_update
    raise e
frappe.exceptions.ValidationError: item_code field cannot be set as unique in tabItem, as there are non-unique existing values`Preformatted text`
cd ~/frappe-bench # or your bench path
cd apps/frappe && git fetch upstream && git checkout master && cd -
cd apps/erpnext && git fetch upstream && git checkout master && cd -
bench update --requirements
bench update --patch

post error logs to pastebin, so that I can help troubleshoot.

Run following sequence, your frappe database .sql file was not able to process properly.

cd ~/frappe-bench # or your bench path
cd apps/frappe && git clean -df && git fetch upstream && git checkout master && cd -
bench update --requirements
bench update --patch

Next time, also include command followed by error log (if you encounter error)
Please also share following logs, if above cmds fails.

cd ~/frappe-bench # or your bench path
tail 100 ./logs/node-socketio.error.log
tail 100 ./logs/worker.error.log
tail 100 ./logs/web.error.log
tail 100 ./logs/redis-cache.error.log
tail 100 ./logs/redis-socketio.error.log
tail 100 ./logs/web.error.log

None of the commands help in resolution of the issue…

In post # 2 above Sangram’s advice seems worth a try.

anand gives similar advice and also a PR fix to refine a validation to prevent that case.

To identify exactly what non-unique Item records are causing the problem might help clarify your problem case. To inspect the database would require sql skills.

hey i hv same problem here https://discuss.frappe.io/t/get-error-updating-v11-migrating-the-doctype-item/46579

any solution yet?

Use bench mariadb.

Use following command

select item_code from `tabItem` group by item_code having count(*) > 1;

This command will give you items which aren’t unique. The use bench console to debug.

2 Likes
MariaDB [c578196c85e90dce]> select item_code from `tabItem` group by item_code having count(*) > 1;
+-----------+
| item_code |
+-----------+
| FORMULA80 |
+-----------+

hi @Himanshu_Mishra thanks for helping, this is output i get, i don’t know how to debug it,

SELECT 
    name, item_group, 
    stock_uom, is_stock_item 
FROM 
   `tabItem` 
WHERE item_code="FORMULA80";

Can you run this query as well??

1 Like

yes here what i get

MariaDB [c578196c85e90dce]> SELECT
    ->     name, item_group,
    ->     stock_uom, is_stock_item
    -> FROM
    ->    `tabItem`
    -> WHERE item_code="FORMULA80";
+-------------+------------------------------------+-----------+---------------+
| name        | item_group                         | stock_uom | is_stock_item |
+-------------+------------------------------------+-----------+---------------+
| FORMULA80   | Uninterruptible Power Supply (UPS) | Unit      |             1 |
| FORMULA80-1 | Uninterruptible Power Supply (UPS) | Nos       |             1 |
+-------------+------------------------------------+-----------+---------------+
2 rows in set (0.00 sec)

can you open http://[[yourdomain]]/desk#Form/Item/FORMULA80-1 and check if this item can be deleted?

no i cannot access at all

There’s a file frappe-bench/apps/frappe/frappe/modules/patch_handler.py. In that, there’s a function like

def check_session_stopped():
	if frappe.db.get_global("__session_status")=='stop':
		frappe.msgprint(frappe.db.get_global("__session_status_message"))
		raise frappe.SessionStopped('Session Stopped')

Change it to

def check_session_stopped():
	return
	if frappe.db.get_global("__session_status")=='stop':
		frappe.msgprint(frappe.db.get_global("__session_status_message"))
		raise frappe.SessionStopped('Session Stopped')

then run

sudo supervisorctl restart all

Then open above link again


i got this

open frappe-bench/apps/erpnext/erpnext/stock/doctype/item/item.json
search for item_code, and try finding

{
   "allow_bulk_edit": 0, 
   "allow_in_quick_entry": 0, 
   "allow_on_submit": 0, 
   "bold": 1, 
   "collapsible": 0, 
   "columns": 0, 
   "description": "", 
   "fieldname": "item_code", 
   "fieldtype": "Data", 
   "hidden": 0, 
   "ignore_user_permissions": 0, 
   "ignore_xss_filter": 0, 
   "in_filter": 0, 
   "in_global_search": 1, 
   "in_list_view": 0, 
   "in_standard_filter": 0, 
   "label": "Item Code", 
   "length": 0, 
   "no_copy": 0, 
   "oldfieldname": "item_code", 
   "oldfieldtype": "Data", 
   "permlevel": 0, 
   "precision": "", 
   "print_hide": 0, 
   "print_hide_if_no_value": 0, 
   "read_only": 0, 
   "remember_last_selected_value": 0, 
   "report_hide": 0, 
   "reqd": 0, 
   "search_index": 0, 
   "set_only_once": 0, 
   "translatable": 0, 
   "unique": 1
  }, 

change unique field to

{
   "allow_bulk_edit": 0, 
   "allow_in_quick_entry": 0, 
   "allow_on_submit": 0, 
   "bold": 1, 
   "collapsible": 0, 
   "columns": 0, 
   "description": "", 
   "fieldname": "item_code", 
   "fieldtype": "Data", 
   "hidden": 0, 
   "ignore_user_permissions": 0, 
   "ignore_xss_filter": 0, 
   "in_filter": 0, 
   "in_global_search": 1, 
   "in_list_view": 0, 
   "in_standard_filter": 0, 
   "label": "Item Code", 
   "length": 0, 
   "no_copy": 0, 
   "oldfieldname": "item_code", 
   "oldfieldtype": "Data", 
   "permlevel": 0, 
   "precision": "", 
   "print_hide": 0, 
   "print_hide_if_no_value": 0, 
   "read_only": 0, 
   "remember_last_selected_value": 0, 
   "report_hide": 0, 
   "reqd": 0, 
   "search_index": 0, 
   "set_only_once": 0, 
   "translatable": 0, 
   "unique": 0
  }, 

Solve the duplicate issue, the stash the changes.

1 Like