I use pgadmin4 and cannot figure out how to make a foreign key. The point is, I have two tables and I need to organize a 1: n relationship between them. Those. The projects table needs to add an attribute that refers to the user table. Tried to do this using the Query Tool, with the string "user_id integer REFERENCES user," does not allow to create the projects table.

CREATE TABLE public.projects ( id integer NOT NULL, name_project character varying(80), attribute character varying(80), value character varying(80), user_id integer REFERENCES user, PRIMARY KEY (id) ) WITH ( OIDS = FALSE ); ALTER TABLE public.projects OWNER to magic; 

    1 answer 1

    You did not write the displayed error message in vain. However, one mistake is striking.

    The word user is reserved by the standard and indicates the current DBMS user. And because REFERENCES user is a syntax error. In order to use this word as an object name, you must use quotes:

     user_id integer REFERENCES "user", 

    Or choose another table name.

    • Indeed, there were not enough quotes, thanks for the reply) - Ilya Kuzmich