GreenDao support for unique constraint on multiple columns
Asked Answered
A

2

6

Does GreenDao supports unique constraint on multiple columns? Equivalent of the following:

create table projects (
  _id integer primary key autoincrement,
  project_type text,
  name text,
  unique (project_type, name)
);
Accoucheur answered 27/2, 2014 at 13:39 Comment(0)
M
13

Yes, it supports.

Create an index with all the properties and make it unique.

Index indexUnique = new Index();
indexUnique.addProperty(project_type);
indexUnique.addProperty(name);
indexUnique.makeUnique();
projectsEntity.addIndex(indexUnique);

Source

Mulligrubs answered 27/2, 2014 at 23:56 Comment(1)
Where or on which file do we need to add those line of codes?Welbie
A
10

As for version 3.2.0, you can declare multiple indexes in the Entity declaration :

@Entity(
    indexes = {
       @Index(value = "column1,column2,column3", unique = true)
    }
)
Aguilera answered 15/3, 2017 at 7:36 Comment(3)
@Jason Robinson Where should I add these lines?Alodium
@135 above your entity class declarationDedradedric
@JasonRobinson i have put like, @Index(value = "fname,category_name", unique = true) is there any other declaration for UNIQUEAlodium

© 2022 - 2024 — McMap. All rights reserved.