开发者

sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating

I'm trying to create a foreing key. After checking the manuals 1 and 2 out i wrote this:

db.execute("create table if not exists table1 (id integer PRIMARY KEY, somedata integer)")
db.execute("create table if not exists table2 (names text, REFERENCES maintable (id)")

and got this:

sqlite3.OperationalError: near "REFERENCES": syntax error

What did i miss? How can i create the foreing key? Thank you.

sqlite_version is 开发者_开发问答3.7.4


your second ligne is wrong it should be

db.execute("create table if not exists table2 (names text,my_id integer, FOREIGN KEY(my_id) REFERENCES maintable (id))")

as explained in http://www.sqlite.org/foreignkeys.html#fk_basics

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜