【发布时间】:2012-01-31 15:12:23
【问题描述】:
我正在尝试使用 loaddata 命令加载带有 json 转储数据(来自在线数据库)的本地 postgres 数据库。但它因“完整性错误”而失败,因为我在数据库中已经有了一些带有主键的数据。然后我尝试刷新数据库以便在syncdb之后将其变为状态。但它给了我以下消息并且失败了。
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;
我有一个数据库,其中包含多个模型和许多外键关系。根据我在互联网上阅读的内容,我了解到应该使用截断来删除依赖的表。我不太清楚在 dbshell 中使用它的确切语法。
我还以 sudo user 的身份访问 postgresql shell,并尝试使用
删除数据库DROP DATABASE DBNAME
命令。但是数据库仍然存在。
编辑:
感谢 stevejalim 的评论,我能够删除数据库并创建一个新数据库。我再次运行了 syncdb 命令并再次创建了数据库。但是尝试使用 loaddata 命令加载 db 会引发错误
IntegrityError: duplicate key value violates unique constraint
对此的任何帮助将不胜感激。
【问题讨论】:
-
听起来很傻,但是在 DB drop 命令之后有分号吗?
DROP DATABASE DBNAME; -
我的错。我一直以为我走错路了。
-
truncate不会删除任何表。它只会删除所有行。
标签: django postgresql dump