[Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference

42👍

It happens if you ran default auth app migrations and later changed the AUTH_USER_MODEL in settings.py. You can try following:

# comment AUTH_USER_MODEL in settings.py so it points to default User model

python manage.py migrate auth zero

# uncomment to be AUTH_USER_MODEL='recommend.AuthUser'

python manage.py migrate auth

22👍

I delete all the migration files and database and applied it.

Then I could migrate.

8👍

Note : I am using sqlite3 as a database.

Hi,
I easily solved it by deleting all the migration files in the migrations folder except the __init__.py file. And also delete db.sqlite3. Now run the following commands : python manage.py makemigrations and then python manage.py migrate. Now you’ll have to create the super user once again, so for this just type the following commmand : python manage.py createsuperuser. Then it will prompt for username, email and password, so enter your credentials and all will continue to work properly once again I hope that this will be helpful.

Click Here To View Image

2👍

For me helped split on two migration

  1. create new table (with out connection betwin new and old tables and without AUTH_USER_MODEL = 'recommend.authuser')
  2. add AUTH_USER_MODEL to settins.py and other connections with new table

2👍

I had to refer to this stack: to be able to solve that issue.
I simply followed the steps bellow:


Since I am not using sqlite, I made new database configuration that refers to my postgres, in my settings.py:

old database i am not more going to use:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'leszexpert', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '5432', } }


new database I want to start using:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'leszexpert_db', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '5432', } }

As you can see, the difference is the DATABASE NAME (referenced after ENGINE)!!!!!!


  1. I stopped the server and already previously deleted all existing corresponding migrations.
  2. I closed my IDE VScode (I have realised from experience that when something does not take effect, after closing it and reopening it, it most time ends up taking effect)
  3. when I reopened the code editor, I uncomment my models (i commented them previously), then VERY VERY IMPORTANT: DO NOT START OR RUN YOUR SERVER AT THIS POINT. because now you are about to operate on the new database specified above and the issue as you know we are trying to solve is that if you run migration of that type of models after runing the server, it will not create your model…therefore DO NOT RUN SERVER YET
  4. INSTEAD DO THIS: RUN MIGRATIONS AND THEN MIGRATE
  5. ET VOILA (French expression to That is it): your extended user model will be created in your database:

Thank you for reading my answer

👤vially

2👍

I’m using MySQL.

The solution, for me, was to delete the files inside migrations folder (all but __ init__.py), DROP and CREATE database in MySQL Workbench and run python manage.py makemigrations and python manage.py migrate again.

Therefore, I had to create a new superuser and all my data was lost 🙁

0👍

If you are still in development and can take the pain of filling out the db data again then here’s what all you need to know!.

you need to first delete all the files related with db(including sqlite.py if it exists), you can do this by the help of below following ways.

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

Now either delete the entire data,tables of your DB or just Drop the db and make a new one and do the migrations separately for each app and migrate it.

python manage.py makemigrations appname
python manage.py migrate appname

then if you get any error for the session like "ProgrammingError: relation "django_session" does not exist"

then follow these steps below:

python manage.py migrate --fake sessions zero

then your sessions migrate will be

python manage.py showmigrations
sessions
[ ] 0001_initial

then migrate with --fake-initial again
python manage.py migrate --fake-initial

now do try to the runserver again and the problem must have been by gone now, if not then please let me know,I’ll try my best possible way to solve it.

Thanks for looking at this.

-1👍

If your migration works locally and not on server do this for server;

root@abcomp:~/var/www/yourapp$sudo -u postgres psql
#list your databases
postgres=#\l
postgres=#\c your_dbname;
your_db=# delete from django_migrations;
# after pulling the latest working changes
(venv)root@abcomp/var/www/yourapp$./manage.py migrate --fake
👤7guyo

Leave a comment