当前位置: 首页 > 知识库问答 >
问题:

Django 1.7向外键字段添加详细名称时迁移错误

归松
2023-03-14

我已经在我的模式中的一系列FK字段上添加了一些冗长的名称[一直到一系列相关的表],但是当我试图运行

python manage.py migrate

或python管理。py迁移——假的

django.db.utils.IntegrityError:重复键值违反唯一约束django_migrations_pkey详细信息:Key(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.

共有1个答案

齐文林
2023-03-14

弄明白了…我正在向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)
 类似资料:
  • 我花了一整天的时间试图弄清楚下面的迁移发生了什么。 这是表的迁移。我的想法是使用“id_stays_abroad”和“user_id”作为外键。 这是添加外键的另一个迁移 当我运行php artisan迁移时,我遇到了以下错误: SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table

  • 我创建了一个名为用户的表。有一些表被称为公司、名称、部门。我想添加company_id,designation_id,department_id列到用户表作为外键。 我试过了,但没用 当我迁移迁移时,它会显示此错误。 Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表(错误号:150“外键约束格式不正确”)(SQL:alter

  • 我有多个迁移,但我认为与这个问题相关的两个迁移是“作业”和“会话”迁移。 作业迁移

  • 我正在尝试在Laravel中创建外键,但是当我使用迁移表时,出现以下错误: 我的迁移代码如下: 优先级迁移文件 用户迁移文件 任何关于我做错了什么的想法,我现在就想知道,因为我有很多表需要创建,例如用户、客户、项目、任务、状态、优先级、类型、团队。理想情况下,我希望创建使用外键保存此数据的表,即和等。 希望有人能帮助我开始。

  • Laravel5.6运行于:PHP7.2,MariaDB 10.3 当我想为我的表设置外键时,我只是不断地犯这个错误。 在其他表中,由Laravel本身定义的所有id变量和无符号自动增量 所以,我的迁移是这样的: 错误如下所示: SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以了解在LINE 1(SQL:alter

  • 我试图在Laravel中创建外键,但是当我使用artisan迁移我的表时,我被抛出以下错误: 模块迁移表: 课程迁移表: 任何关于我做错了什么的想法,我想现在就得到这个,因为我需要创建很多表,例如用户、客户端、项目、任务、状态、优先级、类型、团队。理想情况下,我想创建包含外键、i... eclients_project和project_tasks等数据的表。 希望有人能帮助我开始。