2011-09-02 152 views
1

我首先在我的一個應用程序中開始使用MySQL,現在我正在考慮從MySQL遷移到PostgreSQL。將Django從MySQL遷移到postgresql

我有南安裝遷移。 當我在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 

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

回答

2

您當前遷移這樣工作:

  1. 阿爾特列 「project_length」 爲另一種類型。

由於您正在進行PostgreSQL不支持的修改,所以已損壞。

您必須修復您的遷移。你可以把它改成下面的遷移(它會工作,但也許可以做更容易):

  1. 創建類型要project_length有另一列project_length_tmp和一些缺省值。
  2. 將數據從列project_length遷移到project_lenght_tmp(請參閱南文檔中的數據遷移)。
  3. 刪除column project_length。
  4. 將column project_length_tmp重命名爲project_length。

種類複雜的遷移,但它有兩個主要優勢: 1.它將在所有數據庫上工作。 2.它與您的舊遷移兼容,因此您可以覆蓋舊的遷移(更改文件),它會很好。

方法2

您的問題,另一種方法是隻刪除所有遷移,並從頭開始。如果您只有一個項目的部署,它對您而言將會很好。

0

您不提供正在執行的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。