【问题标题】:Django postgres migrate models column discount.category_name_id does not existDjango postgres 迁移模型列 discount.category_name_id 不存在
【发布时间】:2019-03-20 17:14:14
【问题描述】:

您好,我正在尝试将 Django 后端和 postgresql 数据库结合在一起。

这是我的数据库表:

我在 Django 中的 models.py

from django.db import models

# Create your models here.
class Categories(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.TextField(null=True, unique=True)

    class Meta:
        db_table = 'categories'

    def __str__(self):
        return self.name
class Website(models.Model):
    id = models.IntegerField(primary_key=True)
    site = models.TextField(null=True, unique=True)

    class Meta:
        db_table= 'website'

    def __str__(self):
        return self.site

class Discount(models.Model):
    id = models.IntegerField(primary_key=True)
    product_name = models.TextField()
    product_price = models.TextField()
    product_old_price = models.TextField()
    product_link = models.TextField()
    category_name = models.ForeignKey(Categories, on_delete=models.CASCADE, null=True, to_field='name')
    product_site = models.ForeignKey(Website, on_delete=models.CASCADE, null=True, to_field='site')
    product_image = models.TextField()
    class Meta:
        db_table = 'discount'

    def __str__(self):
            return self.product_name

我设法按照教程将 Django 和 postgresql 链接在一起,但是当我第一次尝试迁移数据库时出现此错误:

列 discount.category_name_id 不存在第 1 行: ..."."product_old_price", "discount"."product_link", "discount"... ^ 提示:也许您的意思是引用列“discount.category_name”。

虽然我在 ForeignKeyField 中使用 to_field='name' 将我的外键从 discount.category_name 链接到 categories.name 但不知何故它使用了 discount.category_name_id ?我不知道 category_name_id 在哪里,它不在我的表中

任何帮助将不胜感激!

【问题讨论】:

  • 不要手动创建表;让 ORM 用 makemigrationsmigrate 来做这件事。在某些情况下,您会想要这样做,但这看起来不像是其中之一,而且您似乎也没有做过很多事情。此外,如果一个教程让你这样做......找到另一个。 official one 是一个很好的起点。
  • 你的架构是错误的 - category.name 不是唯一的,所以折扣表应该引用 category.id。
  • 感谢我没有手动创建表的建议。我在 postgres 数据库中使用了预制表并尝试合并 postgres 数据库和 django 数据,以便 django 可以使用 postgresql 作为数据库。在我连接它们之后当我不使用外部连接表时,通过 settings.py 它可以工作。 Django 需要模型中的 ORM 来表示 postgresql 中的数据,所以我尝试在 makemigrations 之后迁移它
  • 等一下,我认为我手动创建它时出错了,但我需要在 django 管理视图中查看来自 postgres 的数据(django 管理视图需要模型,所以我创建模型),这似乎有效直到我将外键约束添加到表中

标签: python django postgresql orm migrate


【解决方案1】:

以下字段未反映在数据库中,如果没有,请检查您的数据库字段名称然后创建一个虚拟迁移并为此编写脚本

    category_name = models.ForeignKey(Categories, on_delete=models.CASCADE, null=True, to_field='name')

【讨论】:

    【解决方案2】:

    我设法通过添加来修复它

    db_column='category_name'

    到 ForeignKey 字段

    看来我需要将我的 Postgresql 表中实际上是外键的列具体到 ORM

    category_name = models.ForeignKey(Categories, on_delete=models.CASCADE, null=True, to_field='name', db_column='category_name')
        product_site = models.ForeignKey(Website, on_delete=models.CASCADE, null=True, to_field='site', db_column='product_site')
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2021-11-29
      • 2021-07-19
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多