英文:
Multiple migrations are created each time in Django
问题
在我的应用程序中有一个模型:
class PutAwayProductsPosition(models.Model):
products = models.ForeignKey(Product, on_delete=models.CASCADE)
put_position = models.CharField(max_length=50, default=0)
is_put = models.BooleanField(default=False)
class PutAway(models.Model):
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
grn = models.ForeignKey("grn.GRN", on_delete=models.CASCADE)
employee_assigned = models.ForeignKey(Employee, on_delete=models.CASCADE)
putaway_id = models.IntegerField(default=0)
products_position = models.ManyToManyField(PutAwayProductsPosition)
completely_executed = models.BooleanField(default=False)
partially_executed = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
scheduled_datetime = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
每次我运行makemigrations
,都会创建一个文件,如下所示,在迁移中:
class Migration(migrations.Migration):
dependencies = [
('grn', '0068_auto_20230411_0703'),
('putpick', '0033_auto_20230410_0810'),
]
operations = [
migrations.AlterField(
model_name='putaway',
name='grn',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='grn.GRN'),
),
]
即使模型没有更改,我也会迁移它们,然后之后,如果我再次运行makemigrations
,此文件会在文件夹中创建,我无法理解这样做的原因。
我尝试伪造迁移,但得到了这个:
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py migrate --fake putpick 0034_auto_20230411_0703
Operations to perform:
Target specific migration: 0034_auto_20230411_0703, from putpick
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py makemigrations
Migrations for 'grn':
grn/migrations/0069_auto_20230411_0828.py
- Alter field grn on grntempscans
Migrations for 'putpick':
putpick/migrations/0035_auto_20230411_0828.py
- Alter field grn on putaway
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py migrate
Operations to perform:
Apply all migrations: all apps name
Running migrations:
Applying grn.0069_auto_20230411_0828... OK
Applying putpick.0035_auto_20230411_0828... OK
现在当我再次运行makemigartions
时,这两个就会被创建。
英文:
I have a model in my app :
class PutAwayProductsPosition(models.Model):
products = models.ForeignKey(Product, on_delete=models.CASCADE)
put_position = models.CharField(max_length=50, default=0)
is_put = models.BooleanField(default=False)
class PutAway(models.Model):
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
grn = models.ForeignKey("grn.GRN", on_delete=models.CASCADE)
employee_assigned = models.ForeignKey(Employee, on_delete=models.CASCADE)
putaway_id = models.IntegerField(default=0)
products_position = models.ManyToManyField(PutAwayProductsPosition)
completely_executed = models.BooleanField(default=False)
partially_executed = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
scheduled_datetime = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
Every time I run makemigrations
, a file is created like the following in migrations
class Migration(migrations.Migration):
dependencies = [
('grn', '0068_auto_20230411_0703'),
('putpick', '0033_auto_20230410_0810'),
]
operations = [
migrations.AlterField(
model_name='putaway',
name='grn',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='grn.GRN'),
),
]
even when there is no change in the model, I migrate them, and then after that, if I run makemigrations
again this file is created in the folder, I am unable to understand the reason for this.
I tried to fake the migrations but got this :
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py migrate --fake putpick 0034_auto_20230411_0703
Operations to perform:
Target specific migration: 0034_auto_20230411_0703, from putpick
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py makemigrations
Migrations for 'grn':
grn/migrations/0069_auto_20230411_0828.py
- Alter field grn on grntempscans
Migrations for 'putpick':
putpick/migrations/0035_auto_20230411_0828.py
- Alter field grn on putaway
(venv) rahulsharma@Rahuls-MacBook-Air Trakkia-Backend % python manage.py migrate
Operations to perform:
Apply all migrations: all apps name
Running migrations:
Applying grn.0069_auto_20230411_0828... OK
Applying putpick.0035_auto_20230411_0828... OK
and now when I run makemigartions again these two are created.
答案1
得分: 1
显然迁移未能正确执行。
你有一个字段名为 grn,并且并行使用了一个名为 grn 的应用作为外键:
grn = models.ForeignKey("grn.GRN", on_delete=models.CASCADE)
这只是一个猜测,但可能导致迁移执行时出现问题。我会尝试更改字段名。
英文:
obviously the migration does not execute correctly.
You have a field name grn and in parallel an app name grn that you use in the foreign key:
grn = models.ForeignKey("grn.GRN", on_delete=models.CASCADE)
it is just a guess but maybe that causes a problem in the migration execution. I would try and change field name.
答案2
得分: 0
我看到你有多个迁移所需的依赖项。
多个依赖项可能是你遇到的错误生成的原因之一,你可以尝试创建多个不同的迁移,每个迁移只有一个依赖项,然后运行python manage.py makemigrations
。
我最近也遇到了一个错误,它说我要迁移的表已经存在,但我找不到它,所以我试图虚拟迁移它,然后它成功迁移了。
请告诉我这是否有帮助,或者我们可以尝试找出其他解决办法。
英文:
I see you have multiple dependencies for the migration you are trying to make.
Multiple dependencies can be one of the reasons for the generation of the error you are facing, in this case you can either try to make multiple different migrations each with just one dependency and then run python manage.py makemigrations
Even I recently faced an error where it said that the table I am trying to migrate already exists, but I was unable to find it, so I tried to fake migrate it and then it did migrate.
Please let me know if this helps or we can try finding some other way out.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论