IntegrityError 在表“orders_order”上插入或更新违反外键约束“

Posted

技术标签:

【中文标题】IntegrityError 在表“orders_order”上插入或更新违反外键约束“【英文标题】:IntegrityError Insert or update on table "orders_order" violates foreign key constraint " 【发布时间】:2015-08-19 22:18:32 【问题描述】:

我正在尝试在 Django 1.7 中构建一个电子商务网站,一切正常,但当我尝试使用结帐时,我收到以下错误。我不确定它在做什么,因为它在我的本地主机上运行良好,但当我尝试在 webfaction 上部署时却不行。 非常感谢

Environment:


Request Method: GET
Request URL: http://myshoppingapp.com/checkout/

Django Version: 1.7.1
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'accounts',
 'carts',
 'marketing',
 'orders',
 'products',
 'localflavor',
 'stripe')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'marketing.middleware.DisplayMarketing')


Traceback:
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/jamessmith/webapps/myshoppingapp/src/orders/views.py" in checkout
  55.       new_order.save()
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/models/base.py" in save
  591.                        force_update=force_update, update_fields=update_fields)
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/models/base.py" in save_base
  619.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/transaction.py" in __exit__
  339.                         connection.commit()
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/backends/__init__.py" in commit
  176.         self._commit()
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/backends/__init__.py" in _commit
  145.                 return self.connection.commit()
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/jamessmith/webapps/myshoppingapp/lib/python2.7/Django-1.7.1-py2.7.egg/django/db/backends/__init__.py" in _commit
  145.                 return self.connection.commit()

Exception Type: IntegrityError at /checkout/
Exception Value: insert or update on table "orders_order" violates foreign key constraint "billing_address_id_41625ebca5013523_fk_accounts_useraddress_id"
DETAIL:  Key (billing_address_id)=(1) is not present in table "accounts_useraddress".

【问题讨论】:

【参考方案1】:

重要的细节在这一行:

DETAIL:  Key (billing_address_id)=(1) is not present in table "accounts_useraddress".

问题是您试图将订单与数据库中尚不存在的帐单地址相关联。

在您的代码中,您需要确保账单地址已保存到数据库中,然后您才能尝试保存通过外键与其相关的对象。

【讨论】:

您对如何执行此操作有任何建议吗?我正在尝试我觉得很愚蠢:-( 您可以从您正在执行 new_order.save() 的 views.py 文件中发布您的代码的 sn-p 吗?

以上是关于IntegrityError 在表“orders_order”上插入或更新违反外键约束“的主要内容,如果未能解决你的问题,请参考以下文章

mysql IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry '000001.SZ-2018-

ActiveStorage::IntegrityError 何时引发?

IntegrityError:删除时违反外键

IntegrityError 中缺少表名(Django ORM)

无法捕获 SQLAlchemy IntegrityError

Res_Orders_01之需求分析