IntegrityError Insert or update on table "orders_order" violates foreign key constraint "

10,526

The important detail is in this line:

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

The problem is that you are trying to link an order with a billing address which is not yet present in the database.

In your code you'll need to make sure that the billing address is saved to the database before you try and save an object that is related to it by foreign key.

Share:
10,526

Related videos on Youtube

jimmyjump
Author by

jimmyjump

Updated on September 15, 2022

Comments

  • jimmyjump
    jimmyjump over 1 year

    I am trying to build an ecommerce site in Django 1.7 everything works except when I try to use the checkout, I got the following error. I am not sure wht it is doing this as it works fine on my localhost but not when I try to deploy on webfaction. Many thanks

    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".
    
  • jimmyjump
    jimmyjump almost 9 years
    Do you have any suggestions as to how to do this? I'm trying I feel very stupid :-(
  • ChidG
    ChidG almost 9 years
    Can you post a snippet of your code from your views.py file where you are doing new_order.save()?