· 7 years ago · Feb 06, 2019, 03:40 PM
1@Entity(tableName = "notification_targets")
2class Target (
3
4 @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id")
5 var id: Long,
6 var name: String,
7 var hash: String
8)
9
10@Entity(tableName = "notification_group",foreignKeys =
11arrayOf(ForeignKey(entity= Group::class, parentColumns = arrayOf("id"),childColumns = arrayOf("groupId"), onDelete = CASCADE),
12 ForeignKey(entity= Target::class, parentColumns = arrayOf("id"),childColumns = arrayOf("targetId"), onDelete = CASCADE)),
13 indices = [Index("groupId"), Index("targetId")])
14class Group (
15
16 @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id")
17 var id: Int,
18 var groupId: Int?,
19 var targetId: Int?,
20 val name: String
21 )
22
23@Entity(tableName = "notification_category", foreignKeys =
24[ForeignKey(entity= Group::class, parentColumns = arrayOf("id"),childColumns = arrayOf("groupId"), onDelete = ForeignKey.CASCADE)],
25 indices = [Index("groupId")])
26data class Category (
27
28 @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id")
29 var id: Int?,
30 var groupId: Int,
31 val name: String,
32 <other fields>
33)
34
35
36@Dao
37interface TargetDao {
38
39 <other stuff>
40
41 @Delete
42 fun deleteTarget(target: Target)
43}
44
45database = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext<Context>(), Database::class.java).allowMainThreadQueries().build()
46
47CREATE TABLE IF NOT EXISTS `notification_category` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `groupId` INTEGER NOT NULL, `name` TEXT NOT NULL, `expirationMinutes` INTEGER NOT NULL, `maxItems` INTEGER NOT NULL, `showDelete` INTEGER NOT NULL, `allowReadFlag` INTEGER NOT NULL, `showReadFlag` INTEGER NOT NULL, FOREIGN KEY(`groupId`) REFERENCES `notification_group`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )
48
49database.targetDao().deleteTarget(target)
50
51FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)