[英]How to insert a row to table which has a default value in column?
I have such table: 我有这样的表:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: forum; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE forum (
forum_id integer DEFAULT nextval('seq_forum'::regclass) NOT NULL,
forum_name character varying NOT NULL,
group_id integer NOT NULL,
forum_parent integer DEFAULT (-1)
);
ALTER TABLE public.forum OWNER TO postgres;
--
-- Name: PK_forum; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace:
--
ALTER TABLE ONLY forum
ADD CONSTRAINT "PK_forum" PRIMARY KEY (forum_id);
--
-- Name: FK_group; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY forum
ADD CONSTRAINT "FK_group" FOREIGN KEY (group_id) REFERENCES groups(group_id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: FK_parent; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY forum
ADD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent) REFERENCES forum(forum_id);
--
-- PostgreSQL database dump complete
--
As you see above, this table has (at least should have ...) a default value in column forum_parent. 如上所示,此表在forum_parent列中具有(至少应具有...)默认值。 I want to insert some data to this table, I do it like this:
我想向该表中插入一些数据,我这样做是这样的:
INSERT INTO forum (forum_name, group_id) VALUES('new forum', 1);
yup, I have a group with id = 1. But this code gives me: 是的,我有一个id = 1的组。但是这段代码给了我:
PostgreSQL error: 23503 ERROR: insert or update on table "forum" violates foreign key constraint "FK_parent"
DETAIL: Key (forum_parent)=(-1) is not present in table "forum".
NOTICE: there is no transaction in progress
How to make it right? 如何使它正确?
Your INSERT
statement is correct. 您的
INSERT
语句是正确的。 When you don't explicity declare the column name on your INSERT
statement, the value that is inserted on the table is the default ( in your case, it's -1
). 当您未在
INSERT
语句上显式声明列名时,表中插入的值为默认值( 在您的情况下为-1
)。
But the problem lies on the referential constraint. 但是问题在于引用约束。 The column
forum_parent
of table forum
is dependent on the values of column forum_id
of the same table. 表
forum
forum_parent
”列取决于同一表的“ forum_id
”列的值。 As this DDL says, 就像这个DDL所说的,
ALTER TABLE ONLY forum
ADD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent)
REFERENCES forum(forum_id);
The INSERT
statement failed during execution because the value -1
is not present on column forum_id
. INSERT
语句在执行期间失败,因为在forum_id
列上不存在值-1
。
My suggestion is to change the default value from -1
to NULL
我的建议是将默认值从
-1
更改为NULL
CREATE TABLE forum
(
forum_id integer DEFAULT nextval('seq_forum'::regclass) NOT NULL,
forum_name character varying NOT NULL,
group_id integer NOT NULL,
forum_parent integer DEFAULT NULL
);
The difference between NULL
and -1
is that NULL
is simply unknow. 之间的区别
NULL
和-1
是NULL
简直是不明。 or the value does not exist while -1
is an existing numeric value. 或
-1
为现有数值时不存在该值。
You added the following constraint: 您添加了以下约束:
ALTER TABLE ONLY forum
ADD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent) REFERENCES forum(forum_id);
Which basically says that forum_parent must match an existing value of forum_id. 基本上说,forum_parent必须匹配forum_id的现有值。 You probably don't have a row with
forum_id = -1
and therefore it fails. 您可能没有
forum_id = -1
的行,因此它失败了。
You'll need to create a row with forum_id = -1
and only then can you use that default value... 您需要创建一个带有
forum_id = -1
的行,然后才能使用该默认值...
Another option is to put the default value to be null since forum_parent
is nullable 另一个选择是将默认值设置为null,因为
forum_parent
是可为空的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.