﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34050	Generated migration file is not detected by django because of the name of newly generated migration file	Bishal Gautam	Adam Johnson	"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.

"	Bug	closed	Migrations	4.1	Normal	fixed	migrations	jacobtylerwalls@… Adam Johnson	Ready for checkin	1	0	0	0	0	0
