【问题标题】:How to stop an endless loop on model migration, 'models have changes that are not yet reflected' - makemigrations > migrate. Same message如何停止模型迁移的无限循环,“模型具有尚未反映的更改” - makemigrations > migrate。相同的消息
【发布时间】:2019-04-30 06:12:36
【问题描述】:

显示迁移:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth

 [X] 0001_initial
 [X] 0002_auto_20190430_1129
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 (no migrations)
curate
 [X] 0001_initial
 [X] 0002_item_tags
django_comments
 [X] 0001_initial
podcast
 [X] 0001_initial
 [X] 0002_auto_20190430_1129
 [X] 0003_auto_20190430_1132
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
taggit
 [X] 0001_initial

如果我运行 migrate(只是为了看看它是否说'没有迁移应用')我得到这个:

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be appl
ied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrat
e' to apply them.

所以我运行 makemigrations:

Migrations for 'podcast':
  podcast\migrations\0004_auto_20190430_1137.py
    - Alter field published_date on show

Showmigrations:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial

 [X] 0002_auto_20190430_1129
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
core
 (no migrations)
curate
 [X] 0001_initial
 [X] 0002_item_tags
django_comments
 [X] 0001_initial
podcast
 [X] 0001_initial
 [X] 0002_auto_20190430_1129
 [X] 0003_auto_20190430_1132
 [ ] 0004_auto_20190430_1137
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
taggit
 [X] 0001_initial

我再次运行迁移:

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
C:\Users\phill\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\__init
__.py:1421: RuntimeWarning: DateTimeField Show.published_date received a naive datetime (2
019-04-30 11:32:39.288026) while time zone support is active.
  RuntimeWarning)
C:\Users\phill\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\__init
__.py:1421: RuntimeWarning: DateTimeField Show.published_date received a naive datetime (2
019-04-30 11:37:23.102936) while time zone support is active.
  RuntimeWarning)
  Applying podcast.0004_auto_20190430_1137... OK

但是,如果我再次运行 migrate,我会收到相同的消息 - 这是一个循环。

Operations to perform:
  Apply all migrations: accounts, admin, auth, blog, contenttypes, curate, django_comments
, podcast, sessions, sites, taggit
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be appl
ied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrat
e' to apply them.

makemigrations:

Migrations for 'podcast':
  podcast\migrations\0005_auto_20190430_1139.py
    - Alter field published_date on show

这是模型中产生运行时错误的行,我提供了一个默认值,因为它是现有模型上的一个新属性,它需要一个。 Python也提示我用这个,也许这是错误的做法?

published_date = models.DateTimeField(_('Date published'), default=datetime.datetime.today(), null=True, blank=True, help_text=_('The date the feed was published'))

循环迁移的迁移代码在这里:

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('podcast', '0003_auto_20190430_1132'),
    ]

    operations = [
        migrations.AlterField(
            model_name='show',
            name='published_date',
            field=models.DateTimeField(blank=True, default=datetime.datetime(2019, 4, 30, 11, 37, 23, 102936), help_text='The date the feed was published', null=True, verbose_name='Date published'),
        ),
    ]

【问题讨论】:

  • 向我们展示模型和迁移代码。
  • 经过一些滚动后,我现在看到了问题。 default=datetime.datetime.today() 将在每次运行时创建一个新的默认值,新的默认值会在迁移时推送到数据库。尝试不带括号。更改后应该有一个最终迁移。

标签: python django model migration


【解决方案1】:

您需要传递datetime.date.today 的可调用对象,而不是调用它的结果,作为默认值。由于每次结果都会改变,Django 会认为你改变了默认值。

published_date = models.DateTimeField(_('Date published'), default=datetime.datetime.today, null=True, blank=True, help_text=_('The date the feed was published'))
#                                                                                         ^ no parens

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 2015-04-20
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多