无法在 Django 中刷新 Postgres DB

发布于 2025-01-01 14:51:23 字数 3886 浏览 3 评论 0原文

我正在尝试使用 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;

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

我还以 sudo 用户身份访问 postgresql shell 并尝试使用命令删除数据库

DROP DATABASE DBNAME

。但数据库仍然存在。

编辑:

感谢 stevejalim 的评论,我能够删除数据库并创建一个新数据库。我再次运行syncdb命令并再次创建数据库。但是尝试使用 loaddata 命令加载数据库会引发错误

IntegrityError: duplicate key value violates unique constraint

任何对此的帮助将不胜感激。

I am trying to load the local postgres database with json dumped data(from online db) by using the loaddata command. But it fails with "Integrity Error" as I already had some data in the db with the primary key. Then I tried to flush the database so as to take it to state just after syncdb. But it gives me the following message and fails.

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.

This is the corresponding output for sqlflush command

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;

I have a database with multiple models and many foreign key relations among them. From what I read over the internet, I understand that Truncate should used to drop tables which are dependent on. I can't quite figure the exact syntax for using it in the dbshell.

I also accessing the postgresql shell as the sudo user and tried to drop the database using

DROP DATABASE DBNAME

command. But the database persists.

Edit:

Thanks to stevejalim's comment I was able to drop the database and created a new one. I ran syncdb command again and created the db again. But trying to load the db using loaddata command throws the error

IntegrityError: duplicate key value violates unique constraint

Any help on this would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2025-01-08 14:51:23

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

psql postgres

并从那里启动命令:

DROP DATABASE dbname;

查看此 相关问题 od dba.SE 提供删除不想删除的数据库的可靠方法。


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

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

提出问题:您使用哪个版本的 PostgreSQL?
无论如何:检查数据库日志文件中的这些详细信息并修复源中的重复值(或删除 UNIQUE 约束)。

The reason you could not drop the database is probably that you were connected to it. I have to guess because you are holding back on the actual error message. Connect to the default maintenance database postgres instead (as superuser or owner of the db):

psql postgres

And launch the command from there:

DROP DATABASE dbname;

Check out this related question od dba.SE for a robust way to drop a database that doesn't want to go.


Concerning the new error you get: you have a UNIQUE or PRIMARY index or constraint defined on one of your tables but try to insert duplicate values. In modern versions of PostgreSQL you get more information in the error message. Like:

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

Raises the question: which version of PostgreSQL do you use?
In any case: check your database log file for those details and fix the duplicate values in your source (or remove the UNIQUE constraint).

情未る 2025-01-08 14:51:23

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

dropdb dbname

You could try droping the database using the postgres dropdb command line tool.

dropdb dbname
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文