1

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.

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • Does the content in the migration file remain the same every time? – hassansuhaib Apr 11 '23 at 07:32
  • @hassansuhaib yes – Rahul Sharma Apr 11 '23 at 08:00
  • 1
    since migration tells about changing grn ForeignKey, can you pls provide GRN model code as well? – Alexandr Zayets May 18 '23 at 13:53
  • since each time you `migrate` and then afterwards perform a `makemigrations` and it comes back again, can you confirm after looking into the `django_migrations` database table to see if all those migrations you have done are being written in it? Can you try deleting all those entries for those bogus migrations from the `django_migrations` table including the first one and then try `migrate` again – Arun T May 23 '23 at 15:27

2 Answers2

1

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.

Razenstein
  • 3,532
  • 2
  • 5
  • 17
0

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.