#34050: Generated migration file is not detected by django because of the name
of
newly generated migration file
-----------------------------------------+----------------------------
Reporter: Bishal Gautam | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Keywords: migrations
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+----------------------------
After a new project is created we run:
`python manage.py migrate` to sync with our auth database models.
Lets create an app called myapp, register this app on settings. Now we
create a model inside myapp as:
{{{
class MyModel(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
}}}
Lets run `makemigrations` and `migrate` command for the app. This will
generate a migration file with name `0001_initial.py` inside
`myapp/migrations/` folder.
Now let us create a constraints for the model as:
{{{
class MyModel(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Meta:
constraints = [
models.CheckConstraint(
check=models.Q(age__gte=1),
name="Age should not be.less.than.one."
)
]
}}}
When we added CheckConstraint for our previous model, we need to run
`makemigrations` and `migrate` to reflect the new changes on database.
Running makemigrations will generate a migration file inside
`user/migrations/` with name `0002_mymodel_age should not
be.less.than.one..py`. When we try to run migrate, django will not be able
to detect this file and NO changes will be applied to existing database.
If we run makemigrations again, the same file will be generated again. No
matter how many time we run makemigrations, the same file will be
generated again and again. The newly generated migration file is not
detected by django migrate and showmigrations command. This is because of
the name of migration file. The new migration file name contains serveral
dots and because of this name, migrate/showmigration is not able to find
the file.
There are mutiple solutions for this:
- First: Renaming the generated migration file. Remove `.` (dot)
(excluding the .py)from the newly generated migration file. This will make
sure that the file is recognized as python file by django
migrate/showmigrations command.
- Second: We can change the name of our constraint name inside the
migration as :
{{{
class Meta:
constraints = [
models.CheckConstraint(
check=models.Q(age__gte=1),
name="Age should not be less than one"
)
]
}}}
The only difference between the constraint that was above and the
constraint that is implemented now is in the value of name parameter for
models.Q(). We removed the exisiting "dot" `.` from the name. Running
makemigration will generate a migration file with name `0002_mymodel_age
should not be less than one.py`. Now running the migrate/showmigrations
command will find the new migration file. This example is also commited in
the repository[https://github.com/bisalgt/test-django-migrations]. One
can look at the commit history and checkout to proper stage as needed ,
described by commit message on the repository commit history.
- Third: I have replaced the name of the migration file in django source
code so that `.`(dot) gets replaced by some character`_`. I am also
attaching the associated pull request
[https://github.com/django/django/pull/16080].
Using third approach makes sure that no one has to go through the step
First and Second as described above. The solution would work out of the
box.
--
Ticket URL: <https://code.djangoproject.com/ticket/34050>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070183713d57ce-af74006f-1968-4fc1-82f3-59c1dd0dab68-000000%40eu-central-1.amazonses.com.