【发布时间】:2018-03-30 20:32:53
【问题描述】:
我有一个模型 LucyGuide,它通过 OneToOneField 扩展了 Django 的 User 模型:
class LucyGuide(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
还有一个Company 模型,它有一个名为lucy_guide 的字段,它是User 模型的ForeignKey:
class Company(models.Model):
lucy_guide = models.ForeignKey(User)
我想把它改成
class Company(models.Model):
lucy_guide = models.ForeignKey(LucyGuide)
但是,当我实施此更改并进行和运行迁移时,我遇到了IntegrityError:
django.db.utils.IntegrityError: insert or update on table "lucy_web_company" violates foreign key constraint "lucy_web_company_lucy_guide_id_de643702_fk_lucy_web_"
DETAIL: Key (lucy_guide_id)=(461) is not present in table "lucy_web_lucyguide".
这个问题类似于IntegrityError Insert or update on table "orders_order" violates foreign key constraint ";似乎我在将 LucyGuide 对象引用为外键之前创建了它们。
解决此问题的最佳方法是什么?我需要在 shell 中编写一系列命令来创建这些用户吗?
更新
从 shell 中环顾四周,似乎 Django 仍然希望 ForeignKey 的数字 ids 相同,即使模型已经更改(从 User 到 LucyGuide)。这是Users 中的ids,它们也是LucyGuides:
In [11]: lucy_guide_users = [user for user in User.objects.all() if hasattr(user, 'lucyguide')]
In [16]: [user.id for user in lucy_guide_users]
Out[16]: [12, 8, 461, 497, 500, 471, 475, 495]
请注意,这包含来自错误的461 的id。但是,LucyGuides 的 ids 只是
In [17]: [guide.id for guide in LucyGuide.objects.all()]
Out[17]: [1, 2, 3, 4, 5, 6, 7, 8]
似乎解决此问题的方法是更改LucyGuides 的主键,但从What is the best approach to change primary keys in an existing Django app? 看来,这是一个非常复杂的过程。有没有更简单的方法?
【问题讨论】:
-
您不应尝试更改
LucyGuide表中对象的主键。相反,您想更改Company表中的lucy_guide_id值,以便它们引用LucyGuide行。不幸的是,这是一个相当复杂的过程。 question you linked 是关于更改主键的字段类型(例如,从CharField到AutoField),这是一个不同的问题。
标签: python django database postgresql