1

After customizing my user model in Django Oscar, I received the following error message:

IntegrityError at /
insert or update on table "basket_basket" violates foreign key constraint "basket_basket_owner_id_74ddb970811da304_fk_auth_user_id"
DETAIL:  Key (owner_id)=(5) is not present in table "auth_user".

To customize my user model, I followed the instructions here.

First, I wrote the following models.py file, located within my project directory at apps/user/models.py.

from django.db import models
from oscar.apps.customer.abstract_models import AbstractUser
from django.contrib.postgres.fields import ArrayField

class User(AbstractUser):
    acct_bal = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    purchased_items = ArrayField(models.IntegerField(), default=list)

The idea is that I want the user to have an account balance (which I will use for payment later) as well as a list of product numbers representing items that have already been purchased.

After making models.py, I edited the installed apps as follows:

INSTALLED_APPS = [...
'shopworld.apps.user',
] + get_core_apps()

And then put this at the bottom of my settings.py:

AUTH_USER_MODEL = 'user.User'

I then did ./manage.py migrate, but for some reason I am getting this error message. I also tried dropping the django_admin_log table as suggested here, but it did not work. Any help would be greatly appreciated.

Community
  • 1
  • 1
user3487763
  • 31
  • 1
  • 5
  • What error message do you get when you type `./manage.py migrate`? What does `get_core_apps()` do? – Daniil Ryzhkov Dec 14 '15 at 02:20
  • I don't get an error message - it works - but it doesn't solve the IntegrityError. It says no new changes to migrate. get_core_apps() gets the Oscar apps. – user3487763 Dec 14 '15 at 02:23

1 Answers1

1

I fixed this - the issue was that I was trying to migrate to a custom user model after already having done migrations with auth_user. This meant that auth_user didn't update correctly. I had to flush and re-sync the database, so that the initial migration captured the custom user model.

user3487763
  • 31
  • 1
  • 5