Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django keeps creating new migrations even when model has not changed

The following is my model:

class Biovariable(models.Model):
    bioid = models.AutoField(primary_key=True, unique=True)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=2000)
    units = models.ForeignKey(unit, on_delete=models.CASCADE)
    def __str__(self):
        return self.name


class Biovariable_data(models.Model):
    biovar = models.ForeignKey(Biovariable, on_delete=models.CASCADE)
    value = models.CharField(max_length=2000)
    evdate = models.CharField(max_length=30)
    evtime = models.CharField(max_length=30)
    linkedcustomer = models.ForeignKey(customer, on_delete=models.CASCADE)
    linkeddoctor = models.ForeignKey(doctor, on_delete=models.CASCADE)
    linkedclinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)
    class Meta:
        unique_together = [
            "biovar", "value", "evdate", "evtime", "linkedcustomer",
            "linkeddoctor", "linkedclinic"
        ]

Each time I run makemigrations, django keeps creating new migrations, and I cant understand why.

joel@hp:~/myappointments$ ./manage.py makemigrations
Migrations for 'appointments':
appointments/migrations/0010_auto_20190310_2138.py
    - Add field dob to customer
    - Alter field biovar on biovariable_data
joel@hp:~/myappointments$ ./manage.py migrate
Operations to perform:
Apply all migrations: admin, appointments, auth, clinic, contenttypes, sessions
Running migrations:
Applying appointments.0010_auto_20190310_2138... OK
joel@hp:~/myappointments$ ./manage.py makemigrations
Migrations for 'appointments':
appointments/migrations/0011_auto_20190310_2139.py
    - Alter field biovar on biovariable_data
joel@hp:~/myappointments$ ./manage.py makemigrations
Migrations for 'appointments':
appointments/migrations/0012_auto_20190310_2139.py
    - Alter field biovar on biovariable_data
joel@hp:~/myappointments$ ./manage.py migrate
Operations to perform:
Apply all migrations: admin, appointments, auth, clinic, contenttypes, sessions
Running migrations:
Applying appointments.0011_auto_20190310_2139... OK
Applying appointments.0012_auto_20190310_2139... OK
joel@hp:~/myappointments$ ./manage.py makemigrations
Migrations for 'appointments':
appointments/migrations/0013_auto_20190310_2140.py
    - Alter field biovar on biovariable_data
joel@hp:~/myappointments$ 

One of these migration files looks like this:

# Generated by Django 2.1.3 on 2019-03-10 16:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
    dependencies = [
        ('appointments', '0010_auto_20190310_2138'),
    ]
    operations = [
        migrations.AlterField(
            model_name='biovariable_data',
            name='biovar',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='appointments.Biovariable'),
        ),
    ]
like image 890
Joel G Mathew Avatar asked Nov 23 '25 12:11

Joel G Mathew


1 Answers

As mentioned in the comments and documented in similar questions, this is usually because of something dynamic in the model such as a datetime or set of choices that's coming up in an undetermined order.

The issue could also be related to a minor typo or change such as lower vs. upper case characters. In my case, my FK's model initial migration had the name set to a lowercased word rather than the proper upper case name. The referencing model was getting an alter field every time I ran makemigrations. Fixing the FK model's initial migration file by simply capitalizing the name solved the problem:

operations = [
        migrations.CreateModel(
            name='sentence',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

to

operations = [
        migrations.CreateModel(
            name='Sentence',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

Not sure how the lower case character got in there in the first place, though...

It might be a bit difficult to look through every migration file for something like this, however, so I'd recommend just removing all of your migration files, and doing a fresh makemigrations to start over from scratch.

If you'd rather not do this for some reason (migrations have already been synced to git, for example) you could move your migration files to a temporary location, run makemigrations and then see if a git diff shows anything inconsistent in your original and new initial migration files. If you're able to find the issue, restore your original migration files and fix the problem there.

like image 90
lxmmxl56 Avatar answered Nov 25 '25 09:11

lxmmxl56



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!