django将日期字段更改为整数字段无法迁移

时间:2016-05-13 19:29:27

标签: python django postgresql django-models

我最近将日期字段更改为整数字段(数据以剩余月数而非日期指定)。

然而,当我尝试迁移时,make migrations命令工作正常,但是出现了类型转换错误(请注意使用postgres 9.5)。请注意,该模型只有一个实例/条目,我现在也已将其删除,并且由于某种原因迁移仍然失败?

File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: cannot cast type date to integer
LINE 1: ...end_date_fixed" TYPE integer USING "end_date_fixed"::integer
                                                              ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 482, in alter_field
    old_db_params, new_db_params, strict)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 110, in _alter_field
    new_db_params, strict,
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 634, in _alter_field
    params,
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type date to integer
LINE 1: ...end_date_fixed" TYPE integer USING "end_date_fixed"::integer

1 个答案:

答案 0 :(得分:2)

如果表格为空。

通过两个简单的步骤完成迁移。步骤1,从模型中删除现有日期列。第2步添加一个具有相同列名的新INTEGER列。

如果表格不为空。

编辑模型,将end_date_fixed的类型更改为整数。执行make迁移,打开迁移并删除migrations[]内的所有内容,然后将其替换为

 migrations.RunSQL("ALTER TABLE mytable ALTER end_date_fixed TYPE INTEGER USING EXTRACT(epoch FROM end_date_fixed)

尝试此方法时,请确保您没有对模型进行任何其他更改。