giordy16
giordy16

Reputation: 366

How to create double relationship Room

I have to create this relationship in room, but i don't know how to do it.

I created all the tables, but i don't know how to create the double relation between "notification" and "user_access_to_company"

enter image description here

Upvotes: 1

Views: 76

Answers (1)

sergiy tykhonov
sergiy tykhonov

Reputation: 5103

Looks like in user_access_to_company table you have composite primary key (i.e. primary key consists of more than one field).

As for notification table, it looks like it includes a composite foreign key.

Room library supports both types of composite keys:

@Entity(primaryKeys = ["firstName", "lastName"]) // <-- Composite primary key
data class UserAccessToCompany(
    val user_idUser: Int,
    val company_idcompany: Int
)

@Entity(foreignKeys = [ForeignKey(
    entity = UserAccessToCompany::class, 
    parentColumns = arrayOf("user_idUser", "company_idcompany"), 
    childColumns = arrayOf("user_access_to_company_idUser", "user_access_to_company_idcompany"))
]) // <-- Composite primary key
data class Notification(
    @PrimaryKey val idNotification: Int,
    // ...............
    val user_access_to_company_idUser: Int,
    val user_access_to_company_idcompany: Int
)

Upvotes: 1

Related Questions