Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a table with a two or more foreign keys using Android Room?

enter image description here

According the entity-relationship model, the relationship between tbl_post and tbl_category could be specified using Room Persistency Library as follows:

@Entity(foreignKeys = @ForeignKey(
    entity = TblPost.class,
    parentColumns = "id",
    childColumns = "tbl_post_id")
)
class TblPostCategory {
    @PrimaryKey
    public String id;

    @ColumnInfo(name = "user_id")
    public String postId;
}

However TblPostCategory depends on two foreign keys: post_id and category_id from TblPost and TbCategory.

How the relationship should be described using Room annotations?

like image 962
JP Ventura Avatar asked Aug 31 '17 19:08

JP Ventura


People also ask

Can a table has 2 foreign keys?

A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.

What is Android room database?

What is a Room database? Room is a database layer on top of an SQLite database. Room takes care of mundane tasks that you used to handle with an SQLiteOpenHelper . Room uses the DAO to issue queries to its database. By default, to avoid poor UI performance, Room doesn't allow you to issue queries on the main thread.


2 Answers

TblCategory.java

@Entity
class TblCategory {
    @PrimaryKey
    @ColumnInfo(name="cat_id")
    public String id;

    @ColumnInfo(name = "cat_name")
    public String name;
}

TblPost.java (It is missing the foreign key reference but it is not important for the case)

@Entity
class TblPost {
    @PrimaryKey
    @ColumnInfo(name="post_id")
    public String id;

    public String title, content, create_time, author_id;
}

TblPostCategory.java

@Entity(foreignKeys = {
    @ForeignKey(
        entity = TblPost.class,
        parentColumns = "post_id",
        childColumns = "tbl_post_id"
    ),
    @ForeignKey(
        entity = TblCategory.class,
        parentColumns = "cat_id",
        childColumns = "tbl_category_id"
    )
})
class TblPostCategory {
    @PrimaryKey
    @ColumnInfo(name="tbl_post_id")
    public String id;

    @ColumnInfo(name = "tbl_category_id")
    public String categoryId;
}
like image 56
joao86 Avatar answered Sep 18 '22 13:09

joao86


In Kotlin:

@Entity(
    tableName = "some_table",
    indices = [Index("id"), Index("brand_id"), Index("model_id")],
    foreignKeys = [
        ForeignKey(entity = BrandEntity::class, parentColumns = ["id"],
            childColumns = ["brand_id"]),
        ForeignKey(entity = ModelEntity::class, parentColumns = ["id"],
            childColumns = ["model_id"]),
        ForeignKey(entity = Table1Entity::class, parentColumns = ["id"],
            childColumns = ["table1_id"]),
        ForeignKey(entity = Table2Entity::class, parentColumns = ["id"],
            childColumns = ["table2_id"])
    ]
)
like image 22
CoolMind Avatar answered Sep 22 '22 13:09

CoolMind