如何更新并将unit_id设置为NULL,unit id是外键,我需要设置为NULL
CREATE TABLE troops
(
id serial NOT NULL,
unit_id integer,
type integer NOT NULL,
level integer NOT NULL,
CONSTRAINT troops_pkey PRIMARY KEY (id),
CONSTRAINT troops_unit_id_fkey FOREIGN KEY (unit_id)
REFERENCES units (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
OIDS=FALSE
);
我试图更新部队并将 unit_id 设置为 NULL (我在 C++ 中的 pqxx 语句中将 NULL 而不是 0 )但是我得到了类似的错误
insert or update on table "troops" violates foreign key constraint "troops_unit_id_fkey"
DETAIL: Key (unit_id)=(0) is not present in table "units".
当我从 pgadmin 尝试时,我可以在部队中将 unit_id 设置为 NULL,但是 pqxx(从我尝试更新的 c++ 代码)将 NULL 转换为 0,如何解决这个问题?