Multiple migrations are created each time in Django

208 Views Asked by At

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.

2

There are 2 best solutions below

0
Razenstein On BEST ANSWER

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.

3
Ojas Mutreja On

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.