2012-01-31 34 views
2

我正嘗試使用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應該用於刪除依賴於的表。我無法完全確定在dbshel​​l中使用它的確切語法。

我還訪問的PostgreSQL殼作爲sudo user並試圖使用拖放

DROP DATABASE DBNAME 

命令數據庫中。但數據庫依然存在。

編輯:

由於stevejalim的評論我能夠刪除數據庫,並創建一個新的。我再次運行syncdb命令並再次創建數據庫。但是,試圖加載使用loaddata命令分貝引發錯誤

IntegrityError: duplicate key value violates unique constraint 

任何幫助,在此,將不勝感激。

+2

聽起來很愚蠢,但是在DB刪除命令之後你有沒有分號? 'DROP DATABASE DBNAME;' – 2012-01-31 15:25:44

+0

我的不好。我以爲我一直在走錯路。 – primpap 2012-01-31 15:40:49

+1

'truncate'不會刪除任何表。它只會刪除所有行。 – 2012-01-31 16:27:05

回答

6

您無法刪除數據庫的原因可能是您已連接到它。我必須猜測,因爲你在迴避實際的錯誤信息。從那裏

psql postgres 

,併發起命令:連接到默認維護數據庫postgres代替(如數據庫的超級用戶或所有者)

DROP DATABASE dbname; 

看看這個related question od dba.SE一個可靠的方法來刪除不想去的數據庫。


關於你得到新的錯誤:你有你的表中的一個定義的唯一或主索引或約束,但是嘗試插入重複的值。在現代版本的PostgreSQL中,您可以在錯誤消息中獲得更多信息。如:

ERROR: duplicate key value violates unique constraint "tbl_pkey" 
DETAIL: Key (tbl_id)=(10) already exists. 

引發了一個問題:您使用的是哪個版本的PostgreSQL?
在任何情況下:檢查您的數據庫日誌文件的詳細信息,並修復源中的重複值(或刪除UNIQUE約束)。

3

您可以嘗試使用postgres dropdb命令行工具刪除數據庫。

dropdb dbname