我正嘗試使用loaddata命令加載本地postgres數據庫和json轉儲的數據(來自在線數據庫)。但是由於我已經在db中使用主鍵獲得了一些數據,因此它失敗並顯示「完整性錯誤」。然後我嘗試刷新數據庫,以便在syncdb之後立即進入狀態。但它給了我以下信息並失敗。無法在Django中刷新Postgres數據庫
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the 'app' database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Error: Database app couldn't be flushed. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the expected database tables doesn't exist.
* The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "taskapp_taskrequest" references "taskapp_task".
HINT: Truncate table "taskapp_taskrequest" at the same time, or use TRUNCATE ... CASCADE.
這是sqlflush命令
BEGIN;
TRUNCATE "auth_permission", "auth_group", "tagging_taggeditem", "auth_user_user_permissions", "taskapp_task_reviewers", "taskapp_task", "django_site", "profile_profile", "django_content_type", "profile_notification", "django_session", "auth_user_groups", "taskapp_pyntrequest", "profile_rolerequest", "tagging_tag", "taskapp_textbook_chapters", "registration_registrationprofile", "taskapp_textbook", "django_admin_log", "auth_group_permissions", "taskapp_task_selected_users", "taskapp_taskcomment", "south_migrationhistory", "taskapp_task_claimed_users", "taskapp_taskclaim", "taskapp_workreport", "auth_message", "taskapp_reportcomment", "auth_user";
SELECT setval(pg_get_serial_sequence('"registration_registrationprofile"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"tagging_tag"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"tagging_taggeditem"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"south_migrationhistory"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_permission"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_group"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_user"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"auth_message"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"django_content_type"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"django_site"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"django_admin_log"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"profile_profile"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"profile_notification"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"profile_rolerequest"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_task"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_taskcomment"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_taskclaim"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_workreport"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_reportcomment"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_pyntrequest"','id'), 1, false);
SELECT setval(pg_get_serial_sequence('"taskapp_textbook"','id'), 1, false);
COMMIT;
相應的輸出我有多個模型和其中不少外鍵關係數據庫。從我通過互聯網閱讀的內容中瞭解到,Truncate應該用於刪除依賴於的表。我無法完全確定在dbshell中使用它的確切語法。
我還訪問的PostgreSQL殼作爲sudo user並試圖使用拖放
DROP DATABASE DBNAME
命令數據庫中。但數據庫依然存在。
編輯:
由於stevejalim的評論我能夠刪除數據庫,並創建一個新的。我再次運行syncdb命令並再次創建數據庫。但是,試圖加載使用loaddata命令分貝引發錯誤
IntegrityError: duplicate key value violates unique constraint
任何幫助,在此,將不勝感激。
聽起來很愚蠢,但是在DB刪除命令之後你有沒有分號? 'DROP DATABASE DBNAME;' – 2012-01-31 15:25:44
我的不好。我以爲我一直在走錯路。 – primpap 2012-01-31 15:40:49
'truncate'不會刪除任何表。它只會刪除所有行。 – 2012-01-31 16:27:05