Django迁移错误,尝试使用错误的默认值清理日期字段

时间:2017-02-05 02:33:15

标签: django migration

我有一个模特:

class Season(models.Model):
    """ Corresponds to your brochure publishing schedule, e.g. Fall/Winter 2012, Sprint 2014, etc. """
    name = models.CharField(max_length=45, db_index=True, unique=True)
    start_date = models.DateField(db_index=True, help_text="The date enrollment can begin this Season.")

    <snip>

模型在迁移之前,我没有使用South。

start_date字段使用此定义生存多年

start_date = models.DateField(db_index=True, default=False,
                                  help_text="The date of the first Session of this Season.")

我实际上是去编辑帮助文本并注意到默认=错误并认为,这对日期字段没有意义,它一直是我愚蠢时的残余。< / p>

所以我把它拿出来了。

现在我的迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('district', '0012_auto_20160622_1741'),
    ]

    operations = [
        migrations.AlterField(
            model_name='season',
            name='start_date',
            field=models.DateField(help_text=b'The date enrollment can begin this Season.', db_index=True),
        ),
    ]

失败:

 Applying district.0013_auto_20170204_1811...Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 566, in _alter_field
    old_default = self.effective_default(old_field)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
    prepared=False)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
    return self.to_python(value)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1274, in to_python
    parsed = parse_date(value)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/utils/dateparse.py", line 60, in parse_date
    match = date_re.match(value)
TypeError: expected string or buffer

研究 TypeError:期望字符串或缓冲区 会出现关于不执行我想要撤消的内容的讨论,即不设置默认值日期字段与日期不同。

不清楚如何继续这一点。任何提示将不胜感激。

1 个答案:

答案 0 :(得分:1)

  1. 确定数据库中的默认值

    cd /path/to/project
    python manage.py dbshell
    mysql> show create table district_season\G 
    # or for postgres
    # pg_dump -t 'schema.district_season' --schema-only database-name
    
  2. 如果类型没有问题,请使用--fake

    运行迁移
    ./manage.py migrate district --fake
    

    将迁移标记为已运行而未对数据库执行任何操作。