【问题标题】:I changed the primary_key of a model and now it's `ForeignKey` model doesn't work我更改了模型的 primary_key,现在它的“ForeignKey”模型不起作用
【发布时间】:2018-03-24 06:24:10
【问题描述】:

我有一个Post 模型:

class Post(models.Model):
    id = models.CharField(max_length=18, primary_key=True, default=random_string)
    has_upvoted = models.ManyToManyField(User, related_name="has_upvoted")
    has_downvoted = models.ManyToManyField(User, related_name="has_downvoted")

还有一个PostScore 模型:

class PostScore(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    post = models.ForeignKey(Post, related_name='score')

因为我最近将我的Post 模型上的id 从默认的id = models.AutoField(primary_key=True) 更改为当前的CharField,所以在我运行migrate 时会导致此错误:

django.db.utils.ProgrammingError: foreign key constraint "post_post_has_downvoted_post_id_3b2ec618_fk_post_post_id" cannot be implemented
DETAIL:  Key columns "post_id" and "id" are of incompatible types: integer and character varying.

知道如何解决这个问题吗?

编辑:

我现在收到了这个IntegrityError - 我不知道为什么,因为我已经删除了与id 的更改相关的所有迁移历史记录,所以它应该会恢复正常。

这是回溯:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 156, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 256, in remove_field
    self._remake_table(model, delete_field=field)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
    self.quote_name(model._meta.db_table),
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: datatype mismatch

【问题讨论】:

    标签: python django migration


    【解决方案1】:

    问题是,当您将 PK 类型从 AutoField 编辑到 CharField 时,它会尝试从旧值获取新类型并失败。如果您想使用 UUID,请尝试改用 UUIDField

    说实话,你应该完全触摸 id 参数。如果您需要将唯一字符串与您的数据库记录相关联,我建议您添加新字段

    local_id = models.CharField(unique=True, mac_length=120)
    

    如果你仍然需要你的字符串作为PK,即使不推荐,最简单的方法是删除数据库,这样就不需要将integer转换为varchar

    【讨论】:

    • 那么你是说如果我想创建一个自定义的primary_key字段,我不应该叫它id
    • 我说你应该使用默认的PK。如果您仍然需要使用您的,我已经更新了答案。
    • 删除数据库并从头开始创建
    • 那么您是说只有在启动新数据库时才能将 PK 更改为 CharField?我会听取您的建议并使用 local_id 来识别唯一的字母数字字符串 - 以保持 id 完整且不会造成任何问题。
    • 没错。然后批准答案
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2017-10-01
    • 2016-06-30
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多