(迁移表)django.db.utils.IntegrityError:“ id”列中的空值违反了非空约束

时间:2019-11-02 16:22:45

标签: python django postgresql django-migrations

我运行migrate时会出现此错误。我不知道问题出在哪里,因为它是由在migration表内添加migrations数据的内置功能引起的。

Running migrations:
  Applying matches.0023_auto_20191102_1717...Traceback (most recent call last):
  File "/home/milano/.virtualenvs/sport_database/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, matches, 0023_auto_20191102_1717, 2019-11-02 17:17:38.046421+01).


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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/executor.py", line 250, in apply_migration
    self.recorder.record_applied(migration.app_label, migration.name)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 71, in record_applied
    self.migration_qs.create(app=app, name=name)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1285, in execute_sql
    cursor.execute(sql, params)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/milano/.virtualenvs/sport_database/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, matches, 0023_auto_20191102_1717, 2019-11-02 17:17:38.046421+01).

你知道该怎么办吗?我不能只是删除并重新创建数据库。

0 个答案:

没有答案