我正在使用Android的ormlite而我正在尝试获得多列唯一约束。截至目前,我只能对这样的个人专栏进行独特限制:
CREATE TABLE `store_group_item` (`store_group_id` INTEGER NOT NULL UNIQUE ,
`store_item_id` INTEGER NOT NULL UNIQUE ,
`_id` INTEGER PRIMARY KEY AUTOINCREMENT );
我想要的是
CREATE TABLE `store_group_item` (`store_group_id` INTEGER NOT NULL ,
`store_item_id` INTEGER NOT NULL ,
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
UNIQUE( `store_group_id`, `store_item_id` );
在我的模型中,我一直对独特列使用以下注释:
@DatabaseField( unique = true )
有没有办法让它发挥作用?
答案 0 :(得分:14)
如何使用
@DatabaseField (uniqueCombo = true)
String myField;
相反,注释 - 访问表中的项时,uniqueIndexName的速度是否更快?
答案 1 :(得分:7)
修改强>
正如@ Ready4Android指出的那样,我们已经在版本4.20中添加了对uniqueCombo
注释字段的支持。以下是文档:
使用此机制与下面提到的uniqueIndexName
之间应该没有性能差异。
是。您无法使用unique=true
标记执行此操作,但可以使用唯一索引。
@DatabaseField(uniqueIndexName = "unique_store_group_and_item_ids")
int store_group_id;
@DatabaseField(uniqueIndexName = "unique_store_group_and_item_ids")
int store_item_id;
这将创建一个索引来完成唯一性,但我怀疑unique = true无论如何都有隐藏索引。请参阅文档:
我将研究允许多个唯一字段。所有数据库类型可能都不支持。