0

I got this error when I tried to add content or course. It was working fine and now I get this error. I have uploaded it on heroku and it works fine, just in the local dev it won't add anything. Can someone explain whats going wrong. Pls also specify what additional info should I add ?

IntegrityError at /admin/course/course/add/

insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_fkey"
DETAIL:  Key (user_id)=(2) is not present in table "auth_user".

Request Method:     POST
Request URL:    http://127.0.0.1:8888/admin/course/course/add/
Django Version:     1.5.1
Exception Type:     IntegrityError
Exception Value:    

insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_fkey"
DETAIL:  Key (user_id)=(2) is not present in table "auth_user".

Exception Location:     /home/vagrant/sai/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py in _commit, line 240
Python Executable:  /home/vagrant/sai/bin/python
Python Version:     2.7.3
user2539745
  • 995
  • 2
  • 15
  • 24

3 Answers3

5

it is because you changed the AUTH_USER_MODEL after doing the first syncdb.

drop the table "django_admin_log".

then run python manage.py syncdb.

now "django_admin_log" is recreated with the correct reference to the AUTH_USER_MODEL.

panchicore
  • 11,451
  • 12
  • 74
  • 100
0

I just posted an answer here on doing this as a migration instead, which I think is a much better option:

Integrity error on django_admin_log after updating existing site to new Django 1.5 user model

Community
  • 1
  • 1
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
0

I had the same same issue but solved it in the following way:

  1. I simply right-clicked on the SQLite3 folder in my root (base) directory and deleted it.
  2. Confirmed the deletion Note: just make sure you are in the same directory that has the 'manage.py' file. That is, for example (in your terminal, the path points to: C:\users\your_username\desktop\your_project_name>
  3. Ran python manage.py makemigrations
  4. Next, I ran python manage.py migrate
  5. Then ran python manage.py runserver
  6. It was back on. I stopped the server (ctrl + c) so I could create admin logins.
  7. And finally, I created login credentials using python manage.py createsuperuser
  • I logged in to my django admin and it saved successfully what I was saving

Platform: Windows 10 IDE: Pycharm Python version:3.10 Django version: 4.0

I trust this helps. All the best.