【问题标题】:Django unable to load test fixtures, IntegrityErrorDjango 无法加载测试装置,IntegrityError
【发布时间】:2013-11-20 08:46:18
【问题描述】:

我正在使用Saleor/Satchless 来供电和电子商务网站(继承项目)。我找不到 Saleor 邮件列表,所以改为在这里发帖。

I dumped the DB to create some test fixtures

./manage.py dumpdata -e contenttypes -e sessions -e south -e > payments_data.json

当我运行测试并且 django 尝试加载它的固定装置时,它会出错。我使用 Postgres 作为数据库,虽然不是很熟悉,但似乎加载数据的顺序可能存在问题

任何想法如何绕过它?

======================================================================
ERROR: test__hometryon_extra_pair_sizes (payment.tests.InterstitialTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 259, in __call__
self._pre_setup()
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 479, in _pre_setup
self._fixture_setup()
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 849, in _fixture_setup
'skip_validation': True,
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 224, in handle
connection.check_constraints(table_names=table_names)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 132, in check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 56, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)
IntegrityError: Problem installing fixtures: insert or update on table "product_ship" violates foreign key constraint "product_ship_product_ptr_id_fkey"
DETAIL:  Key (product_ptr_id)=(1) is not present in table "product_product".

更新: 开始玩 django-fixture-magic 我发现 product.product 实际上并没有被转储,即使我做了 $ python manage.py dumpdata --indent=4 --all -e contenttypes -e sessions -e south > fulldb.json

更新 2: 在转储数据中包含内容类型后,实际数据中仍然缺少 product.product。我必须为 product.product 和 order_deliverygroup 手动执行 dump_object 和 merge_fixture。加载夹具时,两者都给出了以下类似错误:

IntegrityError: Problem installing fixtures: insert or update on table "order_shippeddeliverygroup" violates foreign key constraint "order_shippeddeliverygroup_deliverygroup_ptr_id_fkey" DETAIL:  Key (deliverygroup_ptr_id)=(1) is not present in table "order_deliverygroup".

我现在在 Contenttypes 上遇到了这个问题

IntegrityError: Problem installing fixture '/Users/andres/Documents/projects/rpmwest/rpmwest/payment/fixtures/payments_data.json': Could not load contenttypes.ContentType(pk=5): duplicate key value violates unique constraint "django_content_type_app_label_model_key"DETAIL:  Key (app_label, model)=(product, digitalship) already exists.

查看夹具中的数据,果然有,但只有其中一个。最初同步模型时是否与创建内容类型的数据库发生冲突?

如果我省略了内容类型,我会收到以下错误:

Traceback (most recent call last):
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 259, in __call__
self._pre_setup()
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 479, in _pre_setup
self._fixture_setup()
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/test/testcases.py", line 849, in _fixture_setup
'skip_validation': True,
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 224, in handle
connection.check_constraints(table_names=table_names)
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 132, in check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 56, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/Users/andres/.virtualenvs/rpmwest/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)
IntegrityError: Problem installing fixtures: insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_content_type_id_fkey" DETAIL:  Key (content_type_id)=(22) is not present in table "django_content_type".

【问题讨论】:

  • 您是否正在运行迁移之类的东西?
  • @limelights 是的,我正在开发数据库上运行迁移,但不在测试中。你是这个意思吗?
  • 供以后参考,如果其他人因为特定错误而登陆这里,请查看stackoverflow.com/a/29670681/940098
  • 您可以在 Saleor github 页面上打开一个问题,Mirumee 团队通常会回复。

标签: python django postgresql testing satchless


【解决方案1】:

似乎 django dumpdata 以错误的顺序转储了固定装置。查看 json 文件以检查 productid: 1 是否存在。如果我认为这是真的,请使用一些更复杂的工具来转储数据,例如django-fixture-magic

或者,您可能希望在上传之前通过 db 引擎删除所有完整性约束,然后尝试重新创建它们,但是如果会出现一些完整性错误,这有点冒险。

对于 PostgreSQL,请咨询 this thread 以了解如何获取表定义。在 MySQL 中它会是这样的:

$ mysqldump --no-data -utest django auth_user_user_permissions
CREATE TABLE `auth_user_user_permissions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `permission_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`permission_id`),
  KEY `auth_user_user_permissions_403f60f` (`user_id`),
  KEY `auth_user_user_permissions_1e014c8f` (`permission_id`),
  CONSTRAINT `user_id_refs_id_dfbab7d` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`),
  CONSTRAINT `permission_id_refs_id_67e79cb` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

以下部分适用于 oracle、postgre 和 mysql

> alter table `auth_user_user_permissions` drop foreign key `user_id_refs_id_dfbab7d`;
Query OK, 0 rows affected (0.97 sec)
Records: 0  Duplicates: 0  Warnings: 0

> alter table `auth_user_user_permissions` add CONSTRAINT `user_id_refs_id_dfbab7d` FOREIGN KEY (`user_id`) references `auth_user` (`id`);
Query OK, 0 rows affected (0.95 sec)
Records: 0  Duplicates: 0  Warnings: 0

【讨论】:

  • 开始玩 github.com/davedash/django-fixture-magic 我发现 product.product 实际上并没有被转储,即使我执行 $ python manage.py dumpdata --indent=4 --all -e contenttypes -e session -e south > fulldb.json 任何想法为什么会发生这种情况?
  • 我查看了saleor source,发现ship是从product继承的。不能说从头开始,现在也没有时间测试,到底应该导入还是装船时创建。你用的是什么版本的 django?
  • @Andres,请确认该问题对您来说仍然很重要。如果是,我可以稍后再做一些研究;为此,了解您的 django 版本至关重要。
  • 非常感谢您迄今为止有兴趣提供帮助。这仍然是一个问题。我正在使用 Django 1.5.1
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 2013-10-24
  • 2011-01-29
  • 2016-04-01
  • 1970-01-01
  • 2022-10-06
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多