Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite syntax error, please tell me where it is?

Tags:

sqlite

Can you guys please point me to the error? And, by the way, is there any SQLite syntax highlighter? Thanks.

    sqlite> .schema recordtypes
    CREATE TABLE recordtypes (record_id text primary key);
    sqlite> .schema headers
    CREATE TABLE headers (header_id text primary key);
    sqlite>                                
    sqlite> 
    sqlite> CREATE TABLE record_to_headers (id INTEGER, recordid TEXT, FOREIGN KEY(recordid) REFERENCES recordtypes(record_id), headerid TEXT, FOREIGN KEY(headerid) REFERENCES headers(header_id));
    Error: near "headerid": syntax error
like image 210
Eugene Avatar asked Nov 18 '25 13:11

Eugene


2 Answers

I believe you need to define all your field then map them to foreign keys, as so:

CREATE TABLE record_to_headers (id INTEGER, recordid TEXT, headerid TEXT, FOREIGN KEY(recordid) REFERENCES recordtypes(record_id), FOREIGN KEY(headerid) REFERENCES headers(header_id));

Let me know if that works.

like image 193
Nicolas Rinaudo Avatar answered Nov 21 '25 10:11

Nicolas Rinaudo


Put your constraints after column definitions:

CREATE TABLE record_to_headers (
    id INTEGER,
    recordid TEXT,
    headerid TEXT,
    FOREIGN KEY(recordid) REFERENCES recordtypes(record_id),
    FOREIGN KEY(headerid) REFERENCES headers(header_id)
);
like image 24
Jan Spurny Avatar answered Nov 21 '25 10:11

Jan Spurny