2017-06-23 32 views
0

How to reset the sequence for IDs on PostgreSQL tables如何從Python程序

我知道,我可以從terminal復位,但我想它從Django的Python程序復位設置主鍵序列。我現在試圖使用RAW sql

postgres=# \c uihspot 
You are now connected to database "uihspot" as user "postgres". 
uihspot=# \dt 
        List of relations 
Schema |    Name    | Type | Owner 
--------+------------------------------+-------+---------- 
public | amway_accessrule    | table | postgres 
public | amway_log     | table | postgres 
public | auth_group     | table | postgres 
public | auth_group_permissions  | table | postgres 
public | auth_permission    | table | postgres 
public | auth_user     | table | postgres 
public | auth_user_groups    | table | postgres 
public | auth_user_user_permissions | table | postgres 
public | django_admin_log    | table | postgres 
public | django_content_type   | table | postgres 
public | django_migrations   | table | postgres 
public | django_session    | table | postgres 
public | member_profile_memberprofile | table | postgres 
public | ruckus_login_accesslog  | table | postgres 
public | ruckus_login_membertomac  | table | postgres 
public | sales_sales     | table | postgres 
public | uploaded_files_uploadedfile | table | postgres 
(17 rows) 

uihspot=# select currval('sales_sales_id_seq'); 
ERROR: currval of sequence "sales_sales_id_seq" is not yet defined in this session 
uihspot=# select currval('sales_sales_seq'); 
ERROR: relation "sales_sales_seq" does not exist 
LINE 1: select currval('sales_sales_seq'); 
        ^
uihspot=# select currval('sales_sales_id_seq'); 
ERROR: currval of sequence "sales_sales_id_seq" is not yet defined in this session 
uihspot=# select currval('uihspot_sales_sales_id_seq'); 
ERROR: relation "uihspot_sales_sales_id_seq" does not exist 
LINE 1: select currval('uihspot_sales_sales_id_seq'); 
        ^
uihspot=# select currval('uihspot.sales_sales_id_seq'); 
ERROR: schema "uihspot" does not exist 
LINE 1: select currval('uihspot.sales_sales_id_seq'); 

但我找不到表!

+0

你打算如何使用它?在遷移? –

+0

我的目標是解決通過Django引發的Postgres「數據庫處於恢復模式」。我已經在'/ var/log/postgres'中檢查了日誌文件。當我使用'bulk_create'時會引發。那麼我想這可能是'id'溢出的問題。然後我試圖從python中重置'sequence_id',而不是從終端重置。我上傳1輪會使〜id增加'id' – Sarit

+1

不,你在這兒吠叫錯了樹。該錯誤消息明確表示,您只需要再次嘗試查詢(在您的其他問題中)。但正如我在答案中提到的,通過django objects.bulk_create simple加載數據不是正確的方法。 – e4c5

回答

0

萬一你是開放從外部Python代碼(一次實踐)這樣做: -

您可以使用PG管理3(僅適用於Linux和Windows)。

只需選擇數據庫,然後執行SQL:

ALTER SEQUENCE 「my_table_name_id_seq」 1重啓;

  • 建議:首先嚐試一個副本,然後在主分貝。

我希望這會有幫助。

+0

謝謝你的迴應。我已經通過e4c5得到了我的根本原因的答案 – Sarit