简体   繁体   English

Django Postgres迁移模型列discount.category_name_id不存在

[英]Django postgres migrate models column discount.category_name_id does not exist

Hi i'm trying to combine Django backend and postgresql database together. 嗨,我正在尝试将Django后端和postgresql数据库组合在一起。

Here is my database tables: 这是我的数据库表:

表

My models.py in Django 我在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

I managed to link Django and postgresql together following tutorials but when i try to migrate the database for the first time it come with this error : 在以下教程中,我设法将Django和postgresql链接在一起,但是当我第一次尝试迁移数据库时,它会出现以下错误:

column discount.category_name_id does not exist LINE 1: ..."."product_old_price", "discount"."product_link", "discount"... ^ HINT: Perhaps you meant to reference the column "discount.category_name". 第1行不存在Discount.category_name_id列:...“。” product_old_price“,”折扣“。” product_link“,”折扣“ ... ^提示:也许您打算引用” discount.category_name“列。

i though i linked my foreign key from discount.category_name to categories.name with to_field='name' in the ForeignKeyField but somehow it used the discount.category_name_id ? 我虽然将我的外键从Discount.category_name链接到Categories.name,并且在ForeignKeyField中使用to_field ='name',但是以某种方式使用了Discount.category_name_id? i don't know where the category_name_id is, it's not in my tables 我不知道category_name_id在哪里,它不在我的表中

Any help would be appreciate! 任何帮助将不胜感激!

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

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

I managed to fixed it by adding 我设法通过添加来修复它

db_column='category_name' db_column = 'CATEGORY_NAME'

to the ForeignKey field 到ForeignKey字段

It seems that i need to specific what column that actually is a Foreign key in my Postgresql tables to the ORM 看来我需要具体说明在我的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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM