gpt4 book ai didi

Why insert into the table is causing conflicts with already existing primary keys?(为什么INSERT INSERT会导致与已有的主键冲突?)

转载 作者:bug小助手 更新时间:2023-10-28 10:33:36 25 4
gpt4 key购买 nike



I have this table

我有这张桌子


CREATE TABLE IF NOT EXISTS public.campaign_result
(
id integer NOT NULL DEFAULT nextval('campaign_result_id_seq'::regclass),
state result_state NOT NULL DEFAULT 'processing'::result_state,
data json NOT NULL,
response json,
act result_action NOT NULL DEFAULT 'none'::result_action,
message character varying COLLATE pg_catalog."default",
campaign_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now(),

CONSTRAINT campaign_result_pkey PRIMARY KEY (id),

CONSTRAINT campaign_result_campaign_id_fkey
FOREIGN KEY (campaign_id)
REFERENCES public.campaign (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
)

This table has almost 1200 entries, but when I do inserts into it, I get this error:

这个表有将近1200个条目,但当我向其中插入数据时,我收到以下错误:



Failed due to duplicate key value violates unique constraint "campaign_result_pkey"

DETAIL: Key (id)=(137) already exists.



This is how I am inserting in it:

这就是我要插入的内容:


def campaing_result_insert(item, messsage, campaign_id):
conn = connect_db()
with conn:
cur = conn.cursor()
cur.execute("""INSERT INTO campaign_result(data, Campaign_id, message) VALUES (%s::json, %s, %s) RETURNING id, data;""",
(item, campaign_id, messsage))

return cur.fetchone()

That's really a weird issue. What is the problem?

这真是个奇怪的问题。有什么问题吗?


更多回答

If you tring to insart new values, just make ID auto increment: ID SERIAL PRIMARY KEY This aproach allow you insert data without ID. It will be generated automaticaly.

如果您尝试插入新值,只需使ID自动递增:ID序列主键此方法允许您插入没有ID的数据。它将自动生成。

The sequence campaign_result_id_seq is generating values for id that already exist in the table. The probable cause is that prior inserts explicitly included the id column, so the sequence was not incremented. Set the sequence's last value to something greater than the highest id value in the table.

序列Campaign_Result_id_seq正在为表中已存在的id生成值。可能的原因是以前的插入显式包含id列,因此序列没有递增。将序列的最后一个值设置为大于表中最高id值的值。

Use id int GENERATED ALWAYS AS IDENTITY and forget about sequences.

使用始终生成的id int作为标识,并忘记序列。

When there are already 1200 records and your sequence started using number 1 for the first record, someone messed up with your sequence: Key (id)=(137) would never be used again. Find the name of your sequence and set it to a high enough number, that is no used: SELECT setval('campaign_result_id_seq', 2000);. And make sure nobody can change the setting manually, like this

当已经有1200条记录,并且您的序列开始使用数字1作为第一条记录时,将永远不会再使用有人扰乱您的序列:key(Id)=(137)。找到您的序列的名称,并将其设置为一个足够大的数字,即不使用:选择setval(‘Campaign_Result_id_seq’,2000);。并确保没有人可以手动更改设置,如下所示

alter sequence campaign_result_id_seq restart with 9999 ? Replace 9999 with the max existing value + 1.

ALTER SEQUENCE Campaign_Result_id_seq使用9999重新启动?将9999替换为最大现有值+1。

优秀答案推荐

The following query synchronizes the sequence with the table ID values:

下面的查询使用表ID值对序列进行重命名:


SELECT SETVAL('campaign_result_id_seq', t.max_id)
FROM campaign_result_id_seq s
CROSS JOIN (SELECT max(id) AS max_id
FROM campaign_result) t
WHERE s.last_value < t.max_id;

I've often used this approach when bulk loading data for testing.

在批量加载数据进行测试时,我经常使用这种方法。



As you guys helped in the comments change the value for campaign_result_id_seq was simple. I used pgadmin4 followed these steps to change its value
1

正如你们在注释中帮助更改的那样,Campaign_Result_id_seq的值很简单。我使用pgadmin4按照以下步骤更改其值1



  1. Go to Sequences in your database

  2. Right click on the *_id_seq, in my case it was campaign_result_id_seq got to properties

  3. Go to Definition tab and change the current value.


enter image description here


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com