【问题标题】:Django multiple primary keys for table error表错误的Django多个主键
【发布时间】:2018-12-05 16:34:18
【问题描述】:

我的 Django 应用程序在 localhost 上运行良好,但是当我尝试将其推送到 Heroku 时出现以下错误:

远程:django.db.utils.ProgrammingError:不允许表“Clientes_productlist”的多个主键

我的models.py 文件是:

class ProductList(models.Model):
    id_new = models.IntegerField(primary_key=True)
    sku = models.CharField(max_length=200)
    client = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    name = models.CharField(max_length=256)
    description = models.CharField(max_length=1000)
    storage = models.CharField(max_length=256)
    cost_price = models.CharField(max_length=256)
    sell_price = models.CharField(max_length=256)
    ncm = models.CharField(max_length=256)
    inventory = models.IntegerField(null=True)

    class Meta:
        unique_together = (('sku', 'client'),)

我也在使用 django-import-export 包。因此,我的resources.py 是:

class ProductListResource(resources.ModelResource):

    class Meta:
        model = ProductList
        skip_unchanged = True
        report_skipped = True
        exclude = ('id',)
        import_id_fields = ('sku', 'client',)
        fields = ('sku', 'client', 'name', 'description', 'storage', 'cost_price', 'sell_price', 'ncm', 'inventory',)

是什么导致了这个错误,我该如何解决?

【问题讨论】:

    标签: python django heroku django-import-export


    【解决方案1】:

    要解决此问题,您需要删除存在ProductList 模型的应用程序的migrations 文件夹中的所有迁移文件,__init__.py 文件除外,然后运行./manage.py makemigrations 以生成新的迁移。然后提交这些迁移并将它们推送到 Heroku。

    【讨论】:

    • 太好了,伙计!再次感谢您的帮助!
    • 之后我现在收到此错误:列 Clientes_productlist.id_new 不存在 LINE 1: SELECT "Clientes_productlist"."id_new", "Clientes_productlis... 你对此有任何线索吗?
    • 我假设 Clientes 对 ProductList 有 FK。所以它有一个与 ProductList 迁移相关/依赖的迁移。由于稍后创建了 ProductList 迁移,因此出现了问题。如果您从项目中删除所有迁移文件(迁移目录中的__init__.py 文件除外),然后运行makemigrations 并提交它们,这可能是最好的选择。在提交迁移并在heroku中运行它们之前,还要在heroku中重置数据库。也许这个文档将有助于重置数据库:gist.github.com/zulhfreelancer/ea140d8ef9292fa9165e
    • 现在当我尝试访问任何页面时出现此错误:关系“django_session”不存在第 1 行:...ession_data”,“django_session”。“expire_date” FROM “django_se...
    • 没关系,我刚刚删除了 Heroku 中的整个数据库并推回了。现在一切都很好!
    猜你喜欢
    • 2017-11-05
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多