【问题标题】:Django south from MySQL to postgresqlDjango 南从 MySQL 到 postgresql
【发布时间】:2011-11-09 05:14:44
【问题描述】:

我第一次开始在我的一个应用程序中使用 MySQL,现在我正在考虑从 MySQL 迁移到 PostgreSQL。

我为迁移安装了 South。 当我在 postgres 中设置新数据库时,我成功同步了我的应用程序,并在我最后一次迁移中完全停止。

> project:0056_auto__chg_field_project_project_length
Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_manager(settings)
  File "/Users/ApPeL/.virtualenvs/fundedbyme.com/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/Users/ApPeL/.virtualenvs/fundedbyme.com/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ApPeL/.virtualenvs/fundedbyme.com/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/ApPeL/.virtualenvs/fundedbyme.com/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/south/management/commands/migrate.py", line 105, in handle
    ignore_ghosts = ignore_ghosts,
  File "/Library/Python/2.7/site-packages/south/migration/__init__.py", line 191, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 221, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 292, in migrate_many
    result = self.migrate(migration, database)
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 125, in migrate
    result = self.run(migration)
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 99, in run
    return self.run_migration(migration)
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 81, in run_migration
    migration_function()
  File "/Library/Python/2.7/site-packages/south/migration/migrators.py", line 57, in <lambda>
    return (lambda: direction(orm))
  File "/Users/ApPeL/Sites/Django/fundedbyme/project/migrations/0056_auto__chg_field_project_project_length.py", line 12, in forwards
    db.alter_column('project_project', 'project_length', self.gf('django.db.models.fields.IntegerField')())
  File "/Library/Python/2.7/site-packages/south/db/generic.py", line 382, in alter_column
    flatten(values),
  File "/Library/Python/2.7/site-packages/south/db/generic.py", line 150, in execute
    cursor.execute(sql, params)
  File "/Users/ApPeL/.virtualenvs/fundedbyme.com/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: column "project_length" cannot be cast to type integer

我想知道是否有一些解决方法?

【问题讨论】:

    标签: mysql django postgresql django-south


    【解决方案1】:

    您当前的迁移以这种方式工作:

    1. 将列“project_length”更改为另一种类型。

    因为你正在做 PostgreSQL 不支持的 alter,所以它坏了。

    您必须修复迁移。您可以将其更改为以下迁移(它会起作用,但可能更容易完成):

    1. 使用您希望 project_length 具有的类型和一些默认值创建另一个列 project_length_tmp。
    2. 将数据从列 project_length 迁移到 project_lenght_tmp(请参阅南文档中的数据迁移)。
    3. 删除列 project_length。
    4. 将列 project_length_tmp 重命名为 project_length。

    有点复杂的迁移,但它有两个主要优势: 1. 它适用于所有数据库。 2.它与您的旧迁移兼容,因此您只需覆盖旧迁移(更改文件)就可以了。

    方法 2

    解决您的问题的另一种方法是删除所有迁移并从头开始。如果您的项目只有单一部署,那么它对您来说可以正常工作。

    【讨论】:

      【解决方案2】:

      您没有提供正在执行的 SQL 的任何详细信息,但它似乎不太可能是 ALTER TYPE 失败 - 假设 SQL 是正确的。

      => CREATE TABLE t (c_text text, c_date date, c_datearray date[]);
      CREATE TABLE
      => INSERT INTO t VALUES ('abc','2011-01-02',ARRAY['2011-01-02'::date,'2011-02-03'::date]);
      INSERT 0 1
      => ALTER TABLE t ALTER COLUMN c_text TYPE integer USING (length(c_text));
      ALTER TABLE
      => ALTER TABLE t ALTER COLUMN c_date TYPE integer USING (c_date - '2001-01-01');
      ALTER TABLE
      => ALTER TABLE t ALTER COLUMN c_datearray TYPE integer USING (array_upper(c_datearray, 1));
      ALTER TABLE
      => SELECT * FROM t;
       c_text | c_date | c_datearray 
      --------+--------+-------------
            3 |   3653 |           2
      (1 row)
      

      没有什么你不能做的。我猜你正在使用的这个 Django 模块生成的 SQL 不正确。

      【讨论】:

        猜你喜欢
        • 2012-08-15
        • 2018-02-05
        • 2016-04-21
        • 2017-02-14
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 2012-01-13
        相关资源
        最近更新 更多