【问题标题】:Django move from sqlite to postgres problems with integerDjango 从 sqlite 迁移到 postgres 整数问题
【发布时间】:2016-08-24 05:47:13
【问题描述】:

我在使用 postgres db 时遇到问题。我收到错误“发生服务器错误。请联系管理员。”在页面上。在控制台中,当我尝试 make migrate 命令时收到错误消息:

错误:

Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying core.0002_auto_20160427_1326...Traceback (most recent call last):
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: ERROR:  column "revival" cannot be cast to type integer
HINT: "USING revival::integer".


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

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.2\helpers\pycharm\django_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "C:\Program Files (x86)\Python35-32\Lib\runpy.py", line 182, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Program Files (x86)\Python35-32\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Python35-32\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:/Users/loc/PycharmProjects/CRM\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\management\__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\management\commands\migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\migrations\executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\migrations\executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\migrations\migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\migrations\operations\fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\base\schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\postgresql_psycopg2\schema.py", line 107, in _alter_field
    new_db_params, strict,
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\base\schema.py", line 636, in _alter_field
    params,
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\base\schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ERROR:  column "revival" cannot be cast to type integer
HINT: "USING revival::integer".


Process finished with exit code 1

模型女巫复兴领域:

class Task(models.Model):
    name = models.CharField(max_length=80, blank=True, null=True)
    deadline_date = models.DateField(null=True, blank=True)
    created_date = models.DateTimeField(auto_now_add=True, editable=False)
    finished_date = models.DateTimeField(null=True, blank=True)
    modified_date = models.DateTimeField(auto_now=True, editable=False)
    responsible_group = models.ForeignKey(Group)
    responsible_user = models.ForeignKey(User, blank=True, null=True, related_name='+')
    revival = models.IntegerField(blank=True, null=True)
    creator = models.ForeignKey(User, blank=True, null=True, related_name='+')
    modified_user = models.ForeignKey(User, blank=True, null=True, related_name='+')
    client = models.ForeignKey(Client, blank=True, null=True)
    order = models.OneToOneField(Order, blank=True, null=True, on_delete=models.CASCADE)
    platform = models.ForeignKey(Platform, blank=True, null=True, on_delete=models.PROTECT)
    comment = models.ForeignKey("Comment", blank=True, null=True, related_name='+')
    status = models.ForeignKey(TaskStatus, default=1, on_delete=models.PROTECT)

你能给我一些建议如何解决这个问题吗?

【问题讨论】:

    标签: django database postgresql sqlite django-models


    【解决方案1】:

    我认为 revival 列在具有 Integer 类型之前有另一种类型。所以,我会尝试:

    1) 首先注释掉revival

    2) makemigrations 和迁移

    3) 撤消包含revival

    的注释代码

    4) makemigrations 和迁移

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2023-03-17
      • 2019-05-31
      • 2021-01-11
      • 2014-07-21
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多