django 模型继承 & FK,无法创建唯一索引

django model inheritance & FK, could not create unique index

我希望能够有一个父 class 的外键,从而也允许对子 class 进行查询。所有其他解决方案都是噩梦。 我已经尝试做到这一点(目标也是一个父 class,它有一个我想要关联的子 classes):

class Destination(PolymorphicModel)

class Account(Destination)

class Organization(Destination)

class Person(Destination)

class Transaction(models.Model)
    destination = models.ForeignKey(Destination, verbose_name="Destination", null=True, blank=True,
                                related_name="CompletedTransaction_Destination_FK")

我也在其他地方引用目的地。 这是我尝试迁移时收到的错误消息:

psycopg2.IntegrityError: 无法创建唯一索引 "baseapp_organization_organization_destination_ptr_id_key" 详细信息:密钥 (organization_destination_ptr_id)=(1) 重复。

如果我能到达目的地我会很高兴class

abstract = True

但是我没有外键。我需要能够选择所有这些目的地,并且它们需要在数据库中保持不同的真实模型。

我也尝试过 GenericRelations,但正如我之前所说,事实证明这是一场噩梦。

感觉我可以通过某种方式解决这个错误,有什么帮助吗?

解决方案(我猜它适用于无法创建索引的其他错误)是擦除数据库并删除所有迁移。可以肯定的是,这很痛苦,但现在我可以做到:

destinations = Destination.objects.all()

它会给我所有的对象,根据 django polymorphic