Context
Hello, I'm working with 2 tables related by a foreign key, WorkOrderItemUsage and WorkOrderItemUsageLot. Every workOrderItemUsage can have multiple lots, so it's a 1 to n relationship, to make my work easier I'm implementing a helper entity called WorkOrderItemUsageWithLots, it's used to retrieve all the data from both tables in one go using room @Relation.
Problem
I've tried to use @Relation with a single column and it works correctly, but I need to stablish my relation based on a composite key like the foreign key, I understand there's no support for composite keys using @Relation I'm trying to link it using an index, Am I missing something? Do I have to refuse to use @Relation in favor of good ol SQL joins in queries?
woItemUsagePk in WorkOrderItemUsageWithLotsOptions: woIdItemUsage, itemId, itemNumber, itemThirdNumber, itemDescription, quantityMeasure, branchId, branchDescription, locationId, doseQuantity, doseMeasureUnit, itemAction, itemPlague, itemJustification private java.util.List lotList
@Entity(
primaryKeys = ["itemId", "branchId", "woIdItemUsage"],
foreignKeys = [
ForeignKey(entity = WorkOrder::class,
parentColumns = ["woId"],
childColumns = ["woIdItemUsage"],
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE)],
indices = [Index("woIdItemUsage", "itemId", "branchId", name = "woItemUsagePk", unique = true)]
@Entity(
primaryKeys = ["woIdItemUsageLot", "itemId", "branchId", "lotId"],
foreignKeys = [
ForeignKey(entity = WorkOrderItemUsage::class,
parentColumns = ["woIdItemUsage", "itemId", "branchId"],
childColumns = ["woIdItemUsageLot", "itemId", "branchId"],
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE)],
indices = [Index("woIdItemUsageLot", "itemId", "branchId", name = "woItemUsageLotPk", unique = true)])
class WorkOrderItemUsageWithLots(@Embedded var itemUsage: WorkOrderItemUsage) {
@Relation(parentColumn = "woItemUsagePk", entityColumn= "woItemUsageLotPk")
var lotList: List<WorkOrderItemUsageLot> = emptyList()}
You don't have a woItemUsagePk field declared in your entity class. You must declare a field to hold the value, Room will not dynamically declare it for you.