I use DBSCHEMA tool and want to execute a query but I got this error:
syntax error at or near "id"
position 209
Code:
CREATE TABLE user_info (
id UUID PRIMARY KEY,
full_name VARCHAR NOT NULL,
birthday VARCHAR NOT NULL,
sex VARCHAR NOT NULL,
profile_image VARCHAR,
verified SMALLINT DEFAULT 0,
legend VARCHAR NULL,
FOREIGN KEY id REFERENCES user(id)
)
2
Answers
According to the documentation the column(s) defined for the constraint should be in parenthesis; in addition,
user
should be delimited.fiddle demo
There are several problems with your code.
First, this is not valid SQL:
The column(s) needs to be surrounded with parenthesis (keep in mind that a foreign key could involve more than one column):
Then:
user
is a reserved word in Postgres, so it needs be quoted when used as an identifier – which has the side effect of making the identifier case-sensitive. Assuming that you created this table as all lower-case, you would do:But it would really be a better idea not to use a reserved word as table name: I would recommend renaming the table to something else if you have a chance to.
DB Fiddle