解决ERPNext安装模块(如indian_compliance)时的DuplicateEntryError错误
ERPNext安装模块时DuplicateEntryError的数据库解决方法
问题背景
在Ubuntu虚拟机上安装ERPNext的indian_compliance模块时执行以下命令:
bench get-app https://github.com/resilient-tech/india-compliance.git bench install-app india_compliance
首次安装失败后,home/frappe-bench/apps/目录生成indian_compliance文件夹,重试时出现错误:
raise frappe.DuplicateEntryError(self.doctype, self.name, e) frappe.exceptions.DuplicateEntryError: ('Module Def', 'GST India', IntegrityError(1062, "Duplicate entry 'GST India' for key PRIMARY"))
删除该文件夹后错误依旧,此前安装ERPNext时也曾遇到类似错误:
Installing erpnext... An error occurred while installing erpnext: ('Module Def', 'Accounts', IntegrityError(1062, "Duplicate entry 'Accounts' for key 'PRIMARY'")) Traceback (most recent call last): File "apps/frappe/frappe/model/base_document.py", line 510, in db_insert frappe.db.sql( File "apps/frappe/frappe/database/database.py", line 229, in sql self._cursor.execute(query, values) File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 158, in execute result = self._query(query) File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 325, in _query conn.query(q) File "env/lib/python3.10/site-packages/pymysql/connections.py", line 549, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "env/lib/python3.10/site-packages/pymysql/connections.py", line 779, in _read_query_result result.read() File "env/lib/python3.10/site-packages/pymysql/connections.py", line 1157, in read first_packet = self.connection._read_packet() File "env/lib/python3.10/site-packages/pymysql/connections.py", line 729, in _read_packet packet.raise_for_error() File "env/lib/python3.10/site-packages/pymysql/protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) File "env/lib/python3.10/site-packages/pymysql/err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.IntegrityError: (1062, "Duplicate entry 'Accounts' for key 'PRIMARY'") During handling of the above exception, another exception occurred: Traceback (most recent call last): File "apps/frappe/frappe/commands/site.py", line 463, in install_app _install_app(app, verbose=context.verbose, force=force) File "apps/frappe/frappe/installer.py", line 291, in install_app add_module_defs(name, ignore_if_duplicate=force) File "apps/frappe/frappe/installer.py", line 622, in add_module_defs d.insert(ignore_permissions=True, ignore_if_duplicate=ignore_if_duplicate) File "apps/frappe/frappe/model/document.py", line 279, in insert self.db_insert(ignore_if_duplicate=ignore_if_duplicate) File "apps/frappe/frappe/model/base_document.py", line 537, in db_insert raise frappe.DuplicateEntryError(self.doctype, self.name, e) frappe.exceptions.DuplicateEntryError: ('Module Def', 'Accounts', IntegrityError(1062, "Duplicate entry 'Accounts' for key 'PRIMARY'"))
此前只能通过重建虚拟机解决,现希望通过MySQL数据库处理,已进入数据库:
mysql -u root -p SHOW DATABASES; USE database_name_here; SHOW TABLES;
但不知后续操作,需解决方案。
解决方案
方法1:使用bench强制安装参数(推荐)
无需直接修改数据库,在安装命令中添加--force参数,让安装程序忽略重复的模块定义:
bench install-app india_compliance --force
该参数会告知Frappe安装器遇到重复Module Def条目时跳过插入,直接完成剩余安装步骤。
方法2:手动操作MySQL数据库
若方法1无效,可直接修改数据库:
- 查询重复条目
在当前数据库会话中,执行SQL语句查看目标模块定义:
SELECT * FROM `tabModule Def` WHERE `name` = 'GST India';
(针对之前的Accounts错误,将GST India替换为Accounts即可)
- 删除重复条目
确认条目存在且为无效残留数据后,执行删除命令:
DELETE FROM `tabModule Def` WHERE `name` = 'GST India';
注意:删除前请确保该条目未被其他模块依赖,建议先备份数据库。
- 重新安装模块
退出MySQL,回到frappe-bench目录,重新执行安装命令:
bench install-app india_compliance
额外提示
- 操作数据库前务必备份:
bench backup - 若出现其他重复条目错误,重复上述步骤处理对应条目
- 确认
database_name_here为当前ERPNext站点对应的数据库(通常格式为_[站点名]_)
内容的提问来源于stack exchange,提问作者dodging_bullets
相关产品推荐
相关产品推荐

