2016-07-06 116 views
1

我已經看到了一些關於此的帖子,但目前爲止沒有人幫助我。我有一個工作的Django應用程序,我想從sqlite到postgresql數據庫。爲此,我遵循djangogirls教程,這很棒,直到發生一些錯誤。django sqlite到postgresql遷移時的ProgrammingError

我已經在我的pc上下載了postgresql進行開發,然後我下載並安裝了psycopg2,它工作正常,我更改了我的settings.py數據庫配置,並且我正好在我想遷移到postgres的地方。

他們在本教程中說的是運行「python manage.py migrate」,這對我不起作用,可能是因爲他們認爲它是一個空白的應用程序或其他東西,而且我已經有了與之相關的遷移和模型。

,我讀了運行

python manage.py makemigrations 
python manage.py migrate 

會做的伎倆,但我和我的自定義模型的問題。然後我讀,我可以發表評論指出,用這個問題模型的所有代碼,然後運行

python manage.py makemigrations 
python manage.py migrate --fake 

取消註釋,並運行

python manage.py migrate 

這一點我試過了,但現在我得到了同樣的錯誤作爲我的自定義模型,但對於contenttypes應用程序。

回溯是這樣的:
( 「拉關係 'django_content_type' n'existe PAS」= 「關係 'django_content_type' 不存在」)

Operations to perform: 
    Apply all migrations: auth, contenttypes, sessions, admin 
Running migrations: 
    No migrations to apply. 
Traceback (most recent call last): 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
psycopg2.ProgrammingError: ERREUR: la relation « django_content_type » n'existe pas 
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co... 
                  ^

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

Traceback (most recent call last): 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\contenttypes\models.py", line 67, in get_for_model 
    ct = self.get(app_label=opts.app_label, model=opts.model_name) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\manager.py", line 122, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 381, in get 
    num = len(clone) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 240, in __len__ 
    self._fetch_all() 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\query.py", line 52, in __iter__ 
    results = compiler.execute_sql() 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql 
    cursor.execute(sql, params) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\utils\six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\db\backends\utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.ProgrammingError: ERREUR: la relation « django_content_type » n'existe pas 
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co... 
                  ^


During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle 
    emit_post_migrate_signal(self.verbosity, self.interactive, connection.alias) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\core\management\sql.py", line 50, in emit_post_migrate_signal 
    using=db) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\dispatch\dispatcher.py", line 192, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\auth\management\__init__.py", line 85, in create_permissions 
    ctype = ContentType.objects.db_manager(using).get_for_model(klass) 
    File "C:\Users\gbastien1\Envs\django-carte\lib\site-packages\django\contrib\contenttypes\models.py", line 80, in get_for_model 
    "Error creating new content types. Please make sure contenttypes " 
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually. 

我的遷移是這樣的:

admin 
[X] 0001_initial 
[X] 0002_logentry_remove_auto_add 
auth 
[X] 0001_initial 
[X] 0002_alter_permission_name_max_length 
[X] 0003_alter_user_email_max_length 
[X] 0004_alter_user_username_opts 
[X] 0005_alter_user_last_login_null 
[X] 0006_require_contenttypes_0002 
[X] 0007_alter_validators_add_error_messages 
contenttypes 
[X] 0001_initial 
[X] 0002_remove_content_type_name 
sessions 
[X] 0001_initial 

我不知道如何解決這個問題,因爲我嘗試了很多東西。數據庫和部署不是我的優點。有人可以幫我解決這個問題嗎?

編輯 我想我所有的變化恢復到我的申請工作,並通過刪除我的源碼文件和我所有的移民開始在部分,我刪除了所有我的pyc(蟒蛇緩存)文件,註釋掉所有代碼與Ecole模型相關,然後再遷移,並且我仍然得到相同的錯誤:(

+0

你想保留你的數據從sqlite? 「python manage.py migrate contenttypes」是做什麼的? –

+0

我不介意從sqlite中丟失我的數據,並且該命令會輸出與上述相同的錯誤:/ –

+0

您可以編輯問題,而不是在答案中添加新的信息。 – marcanuy

回答

0

您可以dump your data into json,刪除您的sqlite文件,然後將您的項目遷移到postgres set up as a DB

工作流程示例可能類似於:

mkdir <app name>/fixtures

python manage.py dumpdata >> <app name>/fixtures/all_data.json

rm <sqlite file>.db

python manage.py migrate

python manage.py loaddata <app name>/fixtures/all_data.json

其中<app name>是您的主應用程序(或任何目錄)的名稱,<sqlite file>是您的sqlite文件的名稱。

+0

這不再工作,然後悲傷的另一種方式:(當我嘗試dumpdata,我得到一個類似於我提到的錯誤: 'CommandError:無法序列化數據庫:錯誤:關係「django_content_type」doesn' t存在' –

+0

你需要保留你的數據嗎 – marcusshep

+0

好吧,現在我想到了,不是真的,因爲我在應用程序的加載中從Excel文件加載我的數據 –

0

那麼,最後我在stackoverflow上發現了一個小帖子,說psycopg2應該在我的settings.py中的INSTALLED_APPS中。我加了它再跑一次

python manage.py migrate 

它工作的很好!感謝那些回答,但真的很感激!

雖然現在我得到了在評論出與「Ecole」模型相關的所有代碼之前得到的錯誤。這是非常相似的contentypes一個:

programmingError: ERROR: relation "carte_interactive_ecole" does not exist 

其中「carte_interactive」是我的應用程序的名字,而「高等」我的模型。

有沒有人有對我另一個想法?當有代碼引用它時,爲什麼我無法遷移我的模型?如果我註釋掉所有高等代碼,這很好......

編輯
我發現,如果我註釋掉的代碼apps.py準備好()我可以遷移我的應用程序,但它可能不因爲當我運行我的應用程序,我做了一些使用數據庫的東西,我得到了與上面相同的錯誤。 :/

+0

嘗試刪除'migrations'文件夾中的所有內容,但__init __。py'文件除外。然後再次運行'python manage.py makemigrations',然後運行'python manage.py migrate'。 –

+0

沒有,沒有工作:/仍然一遍又一遍地得到相同的錯誤.. –

0

所以我張貼此作爲一個答案,因爲它是一個評論太長:

我apps.py註釋掉的代碼,使用我的模型(它可能遷移這樣的模式確實之前使用它。但不存在的。然後我跑makemigrations,和我:

Migrations for 'carte_interactive': 
    001_initial.py: 
     -Create model Ecole 
     -Create model ExcelFile 

這是我的兩個模型,似乎罰款

然後我跑遷移,並得到了

apply all migrations: admin, carte_interactive, [...] 
Running Migrations: No migrations to apply. 
01。

當我showmigrations,一切檢查,以便一切都做似乎,雖然我昨天--fake試圖所以它可能是它檢查了,但並沒有創造一些表...

EDIT(解決方案)
好吧,所以整個問題首先是psycopg2沒有包含在settings.py中的INSTALLED_APPS中,所以無法在postgresql或其他東西中創建表。

而且,我通過徹底刪除數據庫並重新創建它(例如刪除所有表格,但更容易),開始了遷移。然後,另一個問題是apps.py中的代碼在遷移之前運行,所以我必須在運行遷移之前對其進行評論。畢竟,運行makemigrations並使用註釋代碼進行遷移,然後取消註釋並運行該應用程序就像魅力一樣工作!感謝所有回答的人,非常感謝!