开发者

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

开发者 https://www.devze.com 2023-02-21 10:47 出处:网络
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)\")

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

精彩评论

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

关注公众号