gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:03:24 25 4
gpt4 key购买 nike

我已经在我的模式中的一系列 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com