sqlite 多个唯一性不起作用

标签 sqlite unique-constraint

我有一张 table :

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)

但独特的不起作用:

screenshot

最佳答案

我已经测试了您的代码,它按预期工作(测试如下所示)。最有可能发生的事情是表是事先创建的而没有 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 多个唯一性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527312/

相关文章:

c++ - 如何用一个结果集处理程序消除多个相似的 sql 查询?

c - 用C模拟数据库

php - Laravel 表 * 没有名为 * 的列

database - 如何使用 Python3 和 SQlite3 将 3 个列表写入 6 列

java - Servlet 捕获唯一约束异常

node.js - Sequelize 迁移脚本引发错误未处理的拒绝 SequelizeddataBaseError : SQLITE_ERROR: no such table

sql - 如何创建具有唯一组合主键的 Postgres 表?

c# - 使用 SQL Server、MySQL 数据库创建唯一约束

ios - Firebase 中的独特属性

sql - 如何为 Oracle 中的列组合提供唯一约束?