Search code examples
pythondjangoherokurds

Getting Null Value vilates integrity error when registering user or trying to migrate


Recently migrated database to RDS. Using django 2.0 Now when I try to register a user I get a:

 ProgrammingError at /accounts/register/
relation "auth_user" does not exist
LINE 1: SELECT (1) AS "a" FROM "auth_user" WHERE "auth_user"."userna...
                               ^

If I try to manage.py migrate I get the following response:

(boxtrucks-Dleh71wm) bash-3.2$ python manage.py migrate
    Operations to perform:
      Apply all migrations: accounts, admin, auth, contenttypes, saferdb, sessions
    Running migrations:
      Applying accounts.0002_auto_20180131_0135...Traceback (most recent call last):
      File "/Users/Dev/.local/share/virtualenvs/boxtrucks-Dleh71wm/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
        return self.cursor.execute(sql, params)
    psycopg2.IntegrityError: null value in column "id" violates not-null constraint
    DETAIL:  Failing row contains (null, accounts, 0002_auto_20180131_0135, 2018-01-31 20:01:35.801905+00).


    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/Users/Dev/.local/share/virtualenvs/boxtrucks-Dleh71wm/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
        utility.execute()
        return self.cursor.execute(sql, params)
      File "/Users/Dev/.local/share/virtualenvs/boxtrucks-Dleh71wm/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
        raise dj_exc_value.with_traceback(traceback) from exc_value
      File "/Users/Dev/.local/share/virtualenvs/boxtrucks-Dleh71wm/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
        return self.cursor.execute(sql, params)
    django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
    DETAIL:  Failing row contains (null, accounts, 0002_auto_20180131_0135, 2018-01-31 20:01:35.801905+00).

Solution

  • The django_migrations.id column should be auto-incrementing. You'll need to modify this column manually. You may well find the same problem with other models.

    You might find it easier to create a fresh database, run manage.py migrate to create the tables, then finally migrate your data.