Django - makemigrations - No changes detected

前端 未结 30 1304
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 04:44

I was trying to create migrations within an existing app using the makemigrations command but it outputs \"No changes detected\".

Usually I create new apps using the

相关标签:
30条回答
  • 2020-12-02 05:34

    I had copied a table in from outside of django and the Meta class defaulted to "managed = false". For example:

    class Rssemailsubscription(models.Model):
        id = models.CharField(primary_key=True, max_length=36)
        ...
        area = models.FloatField('Area (Sq. KM)', null=True)
    
        class Meta:
            managed = False
            db_table = 'RSSEmailSubscription'
    

    By changing manged to True, makemigrations started picking up changes.

    0 讨论(0)
  • 2020-12-02 05:35

    It is a comment but should probably be an answer.

    Make sure that your app name is in settings.py INSTALLED_APPS otherwise no matter what you do it will not run the migrations.

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    
        'blog',
    ]
    

    Then run:

    ./manage.py makemigrations blog
    
    0 讨论(0)
  • 2020-12-02 05:35

    The possible reason could be deletion of the existing db file and migrations folder you can use python manage.py makemigrations <app_name> this should work. I once faced a similar problem.

    0 讨论(0)
  • 2020-12-02 05:36

    Another possible reason is if you had some models defined in another file (not in a package) and haven't referenced that anywhere else.

    For me, simply adding from .graph_model import * to admin.py (where graph_model.py was the new file) fixed the problem.

    0 讨论(0)
  • 2020-12-02 05:39

    I had another problem not described here, which drove me nuts.

    class MyModel(models.Model):
        name = models.CharField(max_length=64, null=True)  # works
        language_code = models.CharField(max_length=2, default='en')  # works
        is_dumb = models.BooleanField(default=False),  # doesn't work
    

    I had a trailing ',' in one line perhaps from copy&paste. The line with is_dumb doesn't created a model migration with './manage.py makemigrations' but also didn't throw an error. After removing the ',' it worked as expected.

    So be careful when you do copy&paste :-)

    0 讨论(0)
  • 2020-12-02 05:40

    In my case, I first added a field to the model, and Django said there're no changes.

    Than I decided to change the "table name" of the model, makemigrations worked. Than I changed table name back to default, and the new field was also there.

    There is a "bug" in django migration system, sometimes it doesn't see the new field. Might be related with date field.

    0 讨论(0)
提交回复
热议问题