Steven Aguilar
Steven Aguilar

Reputation: 3049

unable to create join table with two foreign keys sqlite3

I get the following error after trying to create a join table with two foreign keys.

Error: near "user_id": syntax error

Here is my code for creating the join table:

sqlite> CREATE TABLE reviews (
   ...> id INTEGER PRIMARY KEY,
   ...> stars INT,
   ...> comment TEXT,
   ...> business_id INT,
   ...> FOREIGN KEY (business_id) REFERENCES businesses(id),
   ...> user_id INT,
   ...> FOREIGN KEY (user_id) REFERENCES users(id)
   ...> );

Here are the other tables:

CREATE TABLE users (
   ...> id INTEGER PRIMARY KEY,
   ...> first_name TEXT,
   ...> last_name TEXT
   ...> );
   
   CREATE TABLE businesses(
   ...> id INTEGER PRIMARY KEY,
   ...> name VARCHAR(250)
   ...> );

Upvotes: 1

Views: 204

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269603

The constraints should go after all the column definitions:

CREATE TABLE reviews (
    id INTEGER PRIMARY KEY,
    stars INT,
    comment TEXT,
    business_id INT,
    user_id INT,
    FOREIGN KEY (business_id) REFERENCES businesses(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

Here is a SQL Fiddle.

Upvotes: 3

Related Questions