我正在尝试从一些数据库备份中恢复我的数据库中的表。备份是通过pgAdmin 4
UI 工具创建的,选择一个表,然后备份它。这是pgAdmin
执行的命令:
--file "C:\\Users\\cchie\\DOCUME~1\\DB_BAC~1\\CURREN~1" --host "localhost" --port "5432" --username "postgres" --no-password --verbose --format=c --blobs --table "public.cosmetic_crawler_currency" "last_cosmetics"
问题是我AutoField
在那个表中有一个,当我恢复一个表时,接下来尝试向其中添加新项目 - 我有这样的错误:
psycopg2.IntegrityError: duplicate key value violates unique constraint "cosmetic_crawler_product_pkey"
DETAIL: Key (product_id)=(27) already exists.
The above exception was the direct cause of the following exception:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "cosmetic_crawler_product_pkey"
DETAIL: Key (product_id)=(27) already exists.
据我了解 - 这意味着 PostgreSQL 服务器不知道我已恢复AutoField
计数器并尝试从 0 开始计数?有没有办法解决这个问题并让它从最后一个数字开始计算?
我被告知我必须使用这个食谱。但是当我运行时manage.py sqlsequencereset my_app_name
- 我看到该命令运行:
BEGIN;
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_product"','product_id'), coalesce(max("product_id"), 1), max("product_id") IS NOT null) FROM "cosmetic_crawler_product";
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_sale"','sale_id'), coalesce(max("sale_id"), 1), max("sale_id") IS NOT null) FROM "cosmetic_crawler_sale";
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_promo"','promo_id'), coalesce(max("promo_id"), 1), max("promo_id") IS NOT null) FROM "cosmetic_crawler_promo";
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_brand_shops"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "cosmetic_crawler_brand_shops";
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_brand_categories"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "cosmetic_crawler_brand_categories";
SELECT setval(pg_get_serial_sequence('"cosmetic_crawler_brand"','brand_id'), coalesce(max("brand_id"), 1), max("brand_id") IS NOT null) FROM "cosmetic_crawler_brand";
COMMIT;
但这仍然没有效果 - 当我添加新项目时 - PostgreSQL 一直从 0 开始计数(更准确地说 - 从之前因 IntegrityError 失败的最后一个数字开始)。
PS 恢复过程与备份相同 - 在pgAdmin
UI 中手动进行。