Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add unique constraint in room database to multiple column

I have one entity in room

@Entity(foreignKeys ={         @ForeignKey(entity = Label.class, parentColumns = "_id", childColumns = "labelId", onDelete = CASCADE),         @ForeignKey(entity = Task.class, parentColumns = "_id", childColumns = "taskId", onDelete = CASCADE) }) public class LabelOfTask extends Data{     @ColumnInfo(name = "labelId")     private Integer labelId;     @ColumnInfo(name = "taskId")     private Integer taskId; } 

sql syntax of this entity is as below

CREATE TABLE `LabelOfTask` (     `_id` INTEGER PRIMARY KEY AUTOINCREMENT,      `labelId` INTEGER,      `taskId` INTEGER,      FOREIGN KEY(`labelId`) REFERENCES `Label`(`_id`) ON UPDATE NO ACTION ON DELETE CASCADE ,      FOREIGN KEY(`taskId`) REFERENCES `Task`(`_id`) ON UPDATE NO ACTION ON DELETE CASCADE  ); 

but what change or annotation I need to add in entity class if I want to append below constraint to the auto generated sql schema of the table

unique (labelId, taskId) 

Ultimately I want to make combination of labelId and taskId unique in a table(or entity of room) using room library.

like image 231
Kevan Avatar asked Feb 24 '18 11:02

Kevan


People also ask

Can we apply unique constraint on multiple columns?

You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

How do I create a unique constraint on multiple columns in MySQL?

The syntax for creating a unique constraint using an ALTER TABLE statement in MySQL is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.

Can unique constraints be multiple?

A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.


2 Answers

A plain UNIQUE constraint on a column, other than via an index, is not supported.

You can enforce this uniqueness property by setting the unique property of an @Index annotation to true. The following code sample (Java) prevents a table from having two rows that contain the same set of values for the firstName and lastName columns:

@Entity(indices = {@Index(value = {"first_name", "last_name"},         unique = true)}) class User {     @PrimaryKey     public int id;      @ColumnInfo(name = "first_name")     public String firstName;      @ColumnInfo(name = "last_name")     public String lastName;      @Ignore     Bitmap picture; } 

The Kotlin equivalent of the annotation is given below:

@Entity(indices = [Index(value = ["first_name", "last_name"], unique = true)]) 

In your code you can do the following changes to have UNIQUE constraints

@Entity(foreignKeys ={         @ForeignKey(entity = Label.class, parentColumns = "_id", childColumns = "labelId", onDelete = CASCADE),         @ForeignKey(entity = Task.class, parentColumns = "_id", childColumns = "taskId", onDelete = CASCADE)},         indices = {@Index(value = {"labelId", "taskId"},                 unique = true)} ) public class LabelOfTask extends Data{     @ColumnInfo(name = "labelId")     private Integer labelId;     @ColumnInfo(name = "taskId")     private Integer taskId; } 
like image 100
lib4 Avatar answered Oct 04 '22 06:10

lib4


If you wonder to make a single column to be unique, only need to write

@Entity(indices = [Index(value = ["name"], unique = true)]) 
like image 22
Ercan Avatar answered Oct 04 '22 04:10

Ercan