Как остановить бесконечный цикл миграции моделей: «в моделях есть изменения, которые еще не отражены» - makemigrations> migrate.То же сообщение - PullRequest
0 голосов
/ 30 апреля 2019

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
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:
  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

Однако, если я снова запускаю миграцию, я получаю то же сообщение - это цикл.

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'),
        ),
    ]

1 Ответ

1 голос
/ 30 апреля 2019

Вам нужно передать вызываемое значение datetime.date.today, а не результат его вызова, по умолчанию.Поскольку результат меняется каждый раз, Джанго будет думать, что вы изменили значение по умолчанию.

published_date = models.DateTimeField(_('Date published'), default=datetime.datetime.today, null=True, blank=True, help_text=_('The date the feed was published'))
#                                                                                         ^ no parens
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...