manage.py migrate不会检测更改

时间:2022-04-03 10:50:05

I have a python project using django restframework. It was initally using an sqlite - database. Now I've changed it to a postgresql. All the data is replaced and the project now uses the postgresql database. It all works fine.

我有一个使用django restframework的python项目。它最初是使用sqlite - 数据库。现在我把它改成了postgresql。所有数据都被替换,项目现在使用postgresql数据库。一切正常。

But now I had to change something in my python project. I had to delete some field in a model (called 'product'). So after it I checked my django restframework in my browser and the field was gone. So the field didn't exist anymore for my backend. All fine but when I tried to add a new product I get an error. Because my database still expects me to fill in the deleted field.

但现在我不得不在我的python项目中改变一些东西。我不得不删除模型中的一些字段(称为“产品”)。所以在我之后我在我的浏览器中检查了我的django restframework并且该字段已经消失了。因此我的后端不再存在这个领域。一切都很好,但当我尝试添加新产品时,我收到了一个错误。因为我的数据库仍然希望我填写已删除的字段。

The field is still in my database but it isn't described in the 0001_initial.py. So I think the makemigrations did its job but the migrate failed?

该字段仍在我的数据库中,但未在0001_initial.py中描述。所以我认为makemigrations完成了它的工作,但迁移失败了?

I already did the following:

我已经做了以下事情:

python manage.py makemigrations
python manage.py migrate

And also

并且

python manage.py makemigrations (changed app)
python manage.py migrate

I really don't know the reason. Because the field is deleted in my project and in the initial.py so I thought the migrate would detect it and change the database.

我真的不知道原因。因为该字段在我的项目和initial.py中被删除所以我认为迁移将检测它并更改数据库。

1 个解决方案

#1


1  

Migrations are a cumulative record. The initial migration describes the state of your database when you initially created it. Subsequent changes are described by further migration files, each of which takes your database to the state at the time that migration was created.

迁移是累积记录。初始迁移描述了最初创建数据库时的状态。后续更改由进一步的迁移文件描述,每个迁移文件都将数据库带到创建迁移时的状态。

So when you run makemigrations, a migration 0002 will be created which will include the code to remove your field.

因此,当您运行makemigrations时,将创建一个迁移0002,其中将包含删除字段的代码。

#1


1  

Migrations are a cumulative record. The initial migration describes the state of your database when you initially created it. Subsequent changes are described by further migration files, each of which takes your database to the state at the time that migration was created.

迁移是累积记录。初始迁移描述了最初创建数据库时的状态。后续更改由进一步的迁移文件描述,每个迁移文件都将数据库带到创建迁移时的状态。

So when you run makemigrations, a migration 0002 will be created which will include the code to remove your field.

因此,当您运行makemigrations时,将创建一个迁移0002,其中将包含删除字段的代码。