Operational Error when linking Auth_User_Model with Foreign Key

I am planning to implement a watchlist for a user which is also used for authentication (AUTH_USER_MODEL). I have set the foreign key as below.

class User(AbstractUser):
    firstName = models.CharField(max_length= 20, null = True)
    lastName = models.CharField(max_length= 20, null=True)
    email = models.EmailField(blank = False, unique = True)
class WatchList(models.Model):
    user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE)
    stocks = models.ManyToManyField(Tickers, related_name='user_watchlist')

It is giving me

OperationalError at /admin/home/watchlist/

#no such column: home_watchlist.user_id

In a different app I am using

class Topic(models.Model):
    title = models.CharField(max_length=70, blank= False, null = False)
    description = RichTextField(null = True, blank = True)
    parent_ticker = models.ForeignKey(Tickers, on_delete= models.SET_NULL, null= True)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.SET_NULL, null= True)
    time = models.DateTimeField(default=datetime.now)
    views = models.IntegerField(default = 0)
    comments = models.IntegerField(default= 0)

    def __str__(self):
        return self.title

which works fine with the foreign key set to settings.Auth_user_model

How can I fix this issue. ?

Yes, I have imported the auth model from settings
I have tried setting it to user model instead but the outcome is same.
When I tried to add to watchlist instead of opening it, the models works fine until I hit save which results in the same error above.

Did you do your makemigrations / migrate on your database to create/update the Watchlist model?

Yes, I have done migrations. I have also deleted and did them again to start from scratch.

And the models you’ve posted here (User and WatchList) - they are complete? There is nothing else in those models? (No Meta class or other class functions?)

Please show your import statements in your models file, along with your AUTH_USER_MODEL setting in your settings file.

Does this error only occur in the admin? If so, please post your admin file.

It may also be helpful if you posted the migration file that creates WatchList.

yes they are completed models no meta classes.

I have noticed that it created migration file in an app that doesn’t have any User, tickers or watchlist models in it but other models that refer to Auth_model.

Here is the migration file from the app I defined User, Watchlist and Tickers in

# Generated by Django 4.2.5 on 2023-11-09 11:46

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('home', '0005_alter_tickers_description'),

    operations = [
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('stocks', models.ManyToManyField(related_name='user_stocks', to='home.tickers')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user', to=settings.AUTH_USER_MODEL)),

here is the settings.py

AUTH_USER_MODEL = 'home.User'

I haven’t implemented data injection from the view, So far the admin is showing error when I tried to access the watchlist but I can see the fields by clicking edit.

I’m a little confused here by what you’ve written. You’re showing two different migration files for the same project, where each is creating a WatchList model? Do you have two WatchList models in your project?

What did you delete?

If you deleted your migration files, did you also drop and recreate the database?

What does the schema look like for the table in your database?


I have tried placing the watchlist models in different app and created migrations but got an error I have updated the comment above.

I have deleted Migration files did not drop any database tables.

Ok, so you have the problem then that your database (and its internal tracking of migrations) are not in sync with the migration files in your system.

You never want to just arbitrarily delete migration files without considering its effects on the migration process.

The easiest solution at this point is to drop the database and recreate it with migrations.

Yes, there are steps you can take to resynchronize things, but there’s no “cookbook” method that I’m aware of that will cover all the potential edge cases, so it tends to being an iterative process with some trial and error.

Sure. Thanks will update.