我有一张桌子:
CREATE TABLE IF NOT EXISTS city_recent
(_id INTEGER PRIMARY KEY AUTOINCREMENT,
city_id INTEGER NOT NULL,
language BOOL NOT NULL,
type BOOL NOT NULL,
FOREIGN KEY (city_id) REFERENCES city(_id),
UNIQUE(city_id, type) ON CONFLICT IGNORE)
但独特的行不通:
最佳答案
我已经测试了您的代码,它可以按预期工作(测试如下所示)。最有可能发生的情况是该表是在没有UNIQUE
约束的情况下预先创建的。尝试删除IF NOT EXISTS
进行确认。
>>> import sqlite3
>>> con = sqlite3.connect(':memory:')
>>> con.execute('''CREATE TABLE IF NOT EXISTS city_recent
... (_id INTEGER PRIMARY KEY AUTOINCREMENT,
... city_id INTEGER NOT NULL,
... language BOOL NOT NULL,
... type BOOL NOT NULL,
... FOREIGN KEY (city_id) REFERENCES city(_id),
... UNIQUE(city_id, type) ON CONFLICT IGNORE);''')
<sqlite3.Cursor object at 0x01298FA0>
>>> con.execute('insert into city_recent(city_id,language,type) values (0,0,1);')
<sqlite3.Cursor object at 0x0129F120>
>>> con.execute('insert into city_recent(city_id,language,type) values (0,0,1);')
<sqlite3.Cursor object at 0x01298FA0>
>>> con.execute('select * from city_recent').fetchall()
[(1, 0, 0, 1)] # -> note that there is only one row in the table
关于sqlite - sqlite多个唯一不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14527312/