Django Programming error column does not exist even after running migrations

前端 未结 9 1046
南旧
南旧 2020-12-08 20:54

I run python manage.py makemigrations and I get: No changes detected Then, python manage.py migrate and I get: No migrations to apply.

9条回答
  •  长情又很酷
    2020-12-08 21:04

    The issue was in the Models for me, for some reason Django was adding '_id' to the end of my Foreign Key column. I had to explicitly set the related named to the Foreign Key. Here 'Cards' is the parent table and 'Prices' is the child table.

    class Cards(models.Model):
        unique_id = models.CharField(primary_key=True, max_length=45)
        name = models.CharField(max_length=225)   
    
    class Prices(models.Model):
            unique_id = models.ForeignKey(Cards, models.DO_NOTHING)
    

    Works after changing to:

    class Cards(models.Model):
        unique_id = models.CharField(primary_key=True, max_length=45)
        name = models.CharField(max_length=225)   
    
    class Prices(models.Model):
            unique_id = models.ForeignKey(Cards, models.DO_NOTHING, db_column='unique_id')
    

提交回复
热议问题