“Unknown column 'column_name' in 'table_name'”error while running django migrate command to rename a field

vmdwslir  于 2023-06-25  发布在  Go
关注(0)|答案(2)|浏览(123)

我正在尝试将一个已经存在的数据库连接到Django。我能够使用inspectDb命令制作模型。我也进行了最初的迁移。当我尝试重命名字段时,问题开始出现。它显示错误“Unknown column 'column_name' in 'table_name'”,即使给定表中的列存在。我想重命名我的字段,并将这些更改反映在我的数据库中。
我从inspect db得到的模型

class ManufacturerModel(models.Model):
    auto_manufacturer_model_name = models.CharField(max_length=255)
    year = models.PositiveSmallIntegerField()
    manufacturer_name = models.CharField(max_length=255)
    manufacturer_id = models.IntegerField(blank=True, null=True)
    manufacturer_year_id = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'manufacturer_model'

更改了我希望在数据库中反映的内容:

class ManufacturerModel(models.Model):
    auto_manufacturer_model_name = models.CharField(max_length=255)
    year = models.PositiveSmallIntegerField()
    manufacturer_name = models.CharField(max_length=255)
    manufacturr_id = models.IntegerField(blank=True, null=True)
    manufacturer_year = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'manufacturer_model'

错误:django.db.utils.OperationalError:(1054,“'manufacturer_model'中的未知列'manufacturer_id '”)
我希望在运行migrate命令后正确地反映数据库更改而不出错。

htrmnn0y

htrmnn0y1#

尝试运行python manage.py makemigrations,然后运行python manage.py migrate

hk8txs48

hk8txs482#

修改模型后,必须在运行python manage.py migrate命令行之前通过运行python manage.py makemigrations应用迁移。

相关问题