How to do multiple column unique-constraint in ormlite ( SQLite )

Pzanno picture Pzanno · Mar 10, 2011 · Viewed 7.6k times · Source

I'm using ormlite for Android and I'm trying to get a multiple column unique-constraint. As of now i'm only able to get a unique constraint on indiviudal columns like this:

CREATE TABLE `store_group_item` (`store_group_id` INTEGER NOT NULL UNIQUE ,
    `store_item_id` INTEGER NOT NULL UNIQUE ,
    `_id` INTEGER PRIMARY KEY AUTOINCREMENT );

and what I want is

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` );

In my model I've been using the following annotations for the unique columns:

@DatabaseField( unique = true )

Is there a way to get this to work?

Answer

Ready4Android picture Ready4Android · Aug 20, 2011

How about using

@DatabaseField (uniqueCombo = true) 
String myField;

annotation instead - is it a matter of the uniqueIndexName being faster when accessing items in the table?