我有这样的表:
--
-- 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
--
正如您在上面看到的,该表在列 forum_parent 中有(至少应该有......)一个默认值。我想在这个表中插入一些数据,我这样做:
INSERT INTO forum (forum_name, group_id) VALUES('new forum', 1);
是的,我有一个 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
如何使它正确?