Django和postgres迁移错误

时间:2017-04-18 10:57:32

标签: django postgresql python-3.x heroku

我在Heroku的django应用程序中创建了一个新的postgressql数据库。但是,每次我进行迁移时,都会收到一条我不理解的错误。

错误:

  Applying argent.0043_auto_20170322_1629...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: cannot cast type date to integer
LINE 1: ...LTER COLUMN "date_id" TYPE integer USING "date_id"::integer,...

我已删除并添加了我唯一的日期字段,但无济于事。无论我的模型中的日期字段是否存在,我仍然会得到相同的错误。任何见解都会非常感激。

models.py

class Entry(models.Model):
    date = models.DateField(blank=True, null=True,)
    euros = models.CharField(max_length=500, blank=True, null=True)
    comments = models.CharField(max_length=900, blank=True, null=True)
    euros_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    xrate = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    dollars_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    daily_savings_dollars = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    daily_savings_display = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)

    def get_absolute_url(self):
        return reverse('argent:detail', kwargs={'pk': self.pk})

    def item_date(self):
        row_title = self.date
        return row_title

    class Meta:

        ordering = ['date']

追溯:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 215, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 513, in alter_field
    old_db_params, new_db_params, strict)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
    new_db_params, strict,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 674, in _alter_field
    params,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 119, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type date to integer
LINE 1: ...LTER COLUMN "date_id" TYPE integer USING "date_id"::integer,...

1 个答案:

答案 0 :(得分:0)

如果要将日期列从整数类型迁移到日期时间,则需要先处理表中的所有现有条目,然后才能运行迁移。 当您尝试更改列类型时,您可能在date列中已经有一些整数,因此转换失败,因为postgres不知道如何将整数转换为日期时间。

如果你不关心它们,你可以删除所有条目,或者你需要将迁移分成不同的步骤:

  1. 使用临时名称
  2. 创建新的datetime列
  3. 运行数据迁移以将整数转换为日期时间并填充新列
  4. 删除旧的整数日期列,重命名新的最终名称。