令人费解的“重复密钥”错误(PostgreSQL)
首先,我要说的是,我了解关系理论,而且我和 MySQL 中的任何人一样有能力,但我完全是 PostgreSQL 菜鸟。
当我尝试将新记录插入到我的 service
表中(仅在生产中)时,我得到这样的信息:
ActiveRecord::RecordNotUnique (PGError: ERROR: duplicate key value violates unique constraint "service_pkey"
: INSERT INTO "service" ("created_at", "name", "salon_id", "updated_at") VALUES ('2011-02-28 02:34:20.054269', 'Manicure', 1, '2011-02-28 02:34:20.054269') RETURNING "id"):
app/controllers/services_controller.rb:46
app/controllers/services_controller.rb:45:in `create'
我不明白为什么。不是应该自动增加我的PK吗?
这是表定义:
snip=> \d service
Table "public.service"
Column | Type | Modifiers
------------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval('service_id_seq'::regclass)
name | character varying(255) |
salon_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"service_pkey" PRIMARY KEY, btree (id)
这是开发中同一个表的定义,它运行良好:
snip_development=> \d service
Table "public.service"
Column | Type | Modifiers
------------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval('service_id_seq'::regclass)
name | character varying(255) |
salon_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"service_pkey" PRIMARY KEY, btree (id)
同样的事情!那么它可能是什么?
First of all, let me say I understand relational theory and I'm as competent as anyone in MySQL but I'm a total PostgreSQL noob.
When I try to insert a new record into my service
table - only in production - I get this:
ActiveRecord::RecordNotUnique (PGError: ERROR: duplicate key value violates unique constraint "service_pkey"
: INSERT INTO "service" ("created_at", "name", "salon_id", "updated_at") VALUES ('2011-02-28 02:34:20.054269', 'Manicure', 1, '2011-02-28 02:34:20.054269') RETURNING "id"):
app/controllers/services_controller.rb:46
app/controllers/services_controller.rb:45:in `create'
I don't understand why. Shouldn't it auto-increment the PK for me?
Here's the table definition:
snip=> \d service
Table "public.service"
Column | Type | Modifiers
------------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval('service_id_seq'::regclass)
name | character varying(255) |
salon_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"service_pkey" PRIMARY KEY, btree (id)
And here's the definition for that same table in development, where it works fine:
snip_development=> \d service
Table "public.service"
Column | Type | Modifiers
------------+-----------------------------+------------------------------------------------------
id | integer | not null default nextval('service_id_seq'::regclass)
name | character varying(255) |
salon_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"service_pkey" PRIMARY KEY, btree (id)
Same thing! So what could it possibly be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能向表中加载了数据,但忽略了将
service_id_seq
的当前值设置为必要的值。您只需SELECT * FROM service_id_seq
即可检查当前值,并使用setval
函数重置它。例如,
SELECT setval('service_id_seq'::regclass, MAX(id)) FROM service;
应将序列重置为表中的当前最大值。You probably loaded up the table with data but neglected to set the current value of
service_id_seq
to the necessary value. You can justSELECT * FROM service_id_seq
to check the current values, and use thesetval
function to reset it.For example,
SELECT setval('service_id_seq'::regclass, MAX(id)) FROM service;
should reset the sequence to the current maximum value from the table.