

But this code gives me: PostgreSQL error: 23503 ERROR: insert or update on table "forum" violates foreign key constraint "FK_parent"ĭETAIL: Key (forum_parent)=(-1) is not present in table "forum". As such, a foreign key references the primary key. 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) A foreign key (FK) is made up of one or more fields in a table that uniquely identify a row in another table. ) a default value in column forum_parent. Then, define the foreign key with an ALTER TABLE statement. In the Console, navigate to the Modify tab of the articles.

Name: FK_parent Type: FK CONSTRAINT Schema: public Owner: postgresĪDD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent) REFERENCES forum(forum_id) Īs you see above, this table has (at least should have. To assign other attributes to the index, create it explicitly using a CREATE INDEX statement. Lets add a foreign-key constraint to the authorid column in the articles table. Name: FK_group Type: FK CONSTRAINT Schema: public Owner: postgresĪDD CONSTRAINT "FK_group" FOREIGN KEY (group_id) REFERENCES groups(group_id) ON UPDATE CASCADE ON DELETE CASCADE Name: PK_forum Type: CONSTRAINT Schema: public Owner: postgres Tablespace:ĪDD CONSTRAINT "PK_forum" PRIMARY KEY (forum_id) The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. create table tablea ( id varchar, somebool bool default false) create table tableb ( id varchar, somebool bool) alter table tableb add constraint tablebunique unique( id, somebool) ALTER TABLE tablea add CONSTRAINT tableatableb FOREIGN. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. INSERT INTO products (name, price) VALUES ('Laptop', 10.0) Also, insert the orders. First, insert the data in the table products by executing the below command. Name: forum Type: TABLE Schema: public Owner: postgres Tablespace:įorum_id integer DEFAULT nextval('seq_forum'::regclass) NOT NULL,ĪLTER TABLE public.forum OWNER TO postgres One way would be to add a dummy column somebool to tablea with a default value of false, then make your FK constraint reference both columns. To check whether the added foreign key constraint is working or not execute the below command.
