1

我已经在我的架构中的一系列 FK 字段中添加了一些详细的名称 [一直通过一系列相关表] 但是当我尝试运行时出现此错误

python manage.py migrate

或 python manage.py migrate --fake

django.db.utils.IntegrityError:重复的键值违反了唯一约束“django_migrations_pkey”详细信息:键(id)=(10)已经存在。

我的数据库中已经有数据了。所有在 Windows 7 的本地机器上运行,在本地主机上使用 postgres。

完整的跟踪如下

C:\Users\aakh\Documents\Gits\testcap>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
        HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://do
cs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
Operations to perform:
  Apply all migrations: tccore, sessions, admin, sites, auth, contenttypes
Running migrations:
  Applying tccore.0006_auto_20150130_1243...Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 108, in apply_migration
    self.recorder.record_applied(migration.app_label, migration.name)
  File "C:\Python27\lib\site-packages\django\db\migrations\recorder.py", line 67, in record_applied
    self.migration_qs.create(app=app, name=name)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 372, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 698, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 731, in _do_insert
    using=using, raw=raw)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 920, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL:  Key (id)=(10) already exists.
4

1 回答 1

-2

想通了...我正在向 ForeignKey 添加详细名称,而它应该已添加到相关对象的相关字段中

正确的:

class AppReference(BaseModel):
    name = models.CharField(max_length=200, unique=True, verbose_name="App name")

class App(BaseModel):

    app_reference = models.ForeignKey(AppReference)
    version = models.CharField(max_length=10)

不正确:

class AppReference(BaseModel):
    name = models.CharField(max_length=200, unique=True)

class App(BaseModel):

    app_reference = models.ForeignKey(AppReference, verbose_name="App name")
    version = models.CharField(max_length=10)
于 2015-01-30T14:22:01.277 回答