python - 向外键字段添加详细名称时出现 django 1.7 迁移错误

标签 python django postgresql django-manage.py

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

python manage.py migrate

或者 python manage.py migrate --fake

django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey" DETAIL: Key (id)=(10) already exists.

我的数据库中已经有数据了。全部在 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.

最佳答案

想通了......我正在向 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)

关于python - 向外键字段添加详细名称时出现 django 1.7 迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28236306/

相关文章:

postgresql - 在函数中使用 plpgsql 复制到 csv 的动态 csv 文件导出错误

sql - POSTGRES 将周列转为行?

python - subprocess.communicate() 向进程发送垃圾?

python - 在 Django 过滤器 orm 中获取当前对象

python - 在 Django 中获取早于 10 天的数据库表数据

python - PyDev 自动完成不起作用

ruby-on-rails - 无法使用 Mavericks 在我的 Mac 上安装 PG gem

python - 如何避免大写首字母缩略词被.capitalize() 小写? Python

python - 将装饰器应用于类中的每个方法?

python - 在异步程序中将 Web 响应写入文件