Emile Paffard-Wray Emile Paffard-Wray - 8 months ago 86
Node.js Question

Postgres syntax error for reference

I am trying to create a table with the following query using the pg npm module (v7):

CREATE TABLE subscriptions(
id SERIAL PRIMARY KEY,
stripe_id VARCHAR(40) UNIQUE NOT NULL,
user INTEGER REFERENCES users,
plan VARCHAR(40) NOT NULL,
active BOOLEAN NOT NULL,
start DATE NOT NULL,
end DATE DEFAULT NULL
);


This seems to match the docs but it is throwing an error:

error: syntax error at or near "user"


The users table has a serial primary key for id, anyone know why this isn't working?

Edit: here's the docs for reference - https://www.postgresql.org/docs/9.4/static/ddl-constraints.html#DDL-CONSTRAINTS-FK

I'm using postgresql version 9.4.

Answer Source

user is a reserved keyword in postgresql. You may use any other column name in its place

Refer the postgresql documentation for the complete list of keywords - Key Words List

According to it, end is also reserved. So the last line of your code will generate an error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download