2014-06-25 18 views
3

非常像主題說。Postgres截斷重啓身份不重啓身份

truncate table "Account" restart identity cascade; 
insert into "Account" ("name", "enabled") values ("test", 1); 
select * from "Account"; 

輸出:

accountId | name | enabled 
-----------+------+--------- 
     14 | test |  1 
(1 row) 

這是表的模式:

         Table "public.Account" 
    Column |   Type   |       Modifiers 
-----------+------------------------+--------------------------------------------------------------- 
accountId | integer    | not null default nextval('"Account_accountId_seq"'::regclass) 
name  | character varying(255) | not null 
enabled | integer    | not null 
Indexes: 
    "Account_pkey" PRIMARY KEY, btree ("accountId") 
Referenced by: 
    TABLE ""AccountPropertyAccess"" CONSTRAINT "AccountPropertyAccess_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("accountId") 
    TABLE ""User"" CONSTRAINT "User_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("accountId") 

這裏有一些多餘的話,因爲堆疊交換認爲我沒有足夠的話,因爲我代碼太多了。

+1

顯示錶模式'\ D 「帳戶」' –

+0

新增表模式。 – bblack

+0

您在第一個代碼塊中的插入語句無效。字符串文字必須使用單引號'「test」'表示列,而不是值。 –

回答

4

看來你沒有創建列作爲serial列,因此Postgres不知道序列「屬於」列,因此「重新啓動身份」不會重置序列。

您可以通過使用serial而不是integer和默認值重新創建表來解決此問題。

或者你可以告訴Postgres的,列「擁有」的順序:

alter sequence "Account_accountId_seq" owned by "Account"."accountId"; 

BTW:使用帶引號的標識符通常是更麻煩比它的價值(在執法機關在我的經驗) 。大多數情況下,最好永遠不要使用帶引號的標識符,例如create table Account (...)代替create table "Account" (...)

1

它看起來像你有一個自動增量序列ID。你也需要重置它。

PostgreSQL文檔shows

ALTER SEQUENCE Account RESTART WITH 1; 

有幾個問題herehere把它。