【问题标题】:Cannot flush Postgres DB in Django无法在 Django 中刷新 Postgres DB
【发布时间】: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;

我有一个数据库,其中包含多个模型和许多外键关系。根据我在互联网上阅读的内容,我了解到应该使用截断来删除依赖的表。我不太清楚在 dbshel​​l 中使用它的确切语法。

我还以 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


【解决方案1】:

您无法删除数据库的原因可能是您已连接到它。我不得不猜测,因为您对实际的错误消息有所保留。改为连接到默认维护数据库postgres(作为数据库的超级用户或所有者):

psql postgres

然后从那里启动命令:

DROP DATABASE dbname;

查看此related question od dba.SE,了解删除不想删除的数据库的可靠方法。


关于您得到的新错误:您在其中一个表上定义了 UNIQUE 或 PRIMARY 索引或约束,但尝试插入重复值。在现代版本的 PostgreSQL 中,您会在错误消息中获得更多信息。喜欢:

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

提出一个问题:您使用哪个版本的 PostgreSQL?
在任何情况下:检查您的数据库日志文件以获取这些详细信息并修复源中的重复值(或删除 UNIQUE 约束)。

【讨论】:

    【解决方案2】:

    您可以尝试使用 postgres dropdb 命令行工具删除数据库。

    dropdb dbname
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2020-12-19
      • 2019-11-05
      • 1970-01-01
      • 2018-10-17
      • 2017-09-04
      • 2018-11-21
      • 2013-05-01
      • 1970-01-01
      相关资源
      最近更新 更多