我在使用以下代码创建的SQLite数据库中有一个表。请注意复合主键:
db.create_table(:person_hash) do
Integer :person_id
Bignum :hash // MD5 hash in hex stored as numeric: hash.to_i(16)
primary_key [:person_id, :hash]
end
此表已有一些行:
puts db[:person_hash].where(:person_id => 285577).all
# {:person_id=>285577, :hash=>306607097659338192312932577746542919680}
现在,当我尝试插入此内容时:
db[:person_hash].insert({:person_id=>285577, :hash=>306607097659338206333361532286405644297})
我明白了:
SQLite3::ConstraintException: columns person_id, hash are not unique (Sequel::DatabaseError)
如果表格中尚不存在该行,那么它又如何重复?
我尝试为同一个人ID插入另一个哈希值,但它没有问题。
答案 0 :(得分:1)
这似乎是SQLite中的一个错误:
$ sqlite3
SQLite version 3.8.9 OpenBSD
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE person_hash (person_id integer, hash bigint, primary key (person_id, hash));
sqlite> INSERT INTO person_hash VALUES (285577, 306607097659338192312932577746542919680);
sqlite> INSERT INTO person_hash VALUES (285577, 306607097659338206333361532286405644297);
Error: UNIQUE constraint failed: person_hash.person_id, person_hash.hash