使用综合索引的Room @ Relation

时间:2019-01-30 15:32:14

标签: android kotlin android-room

上下文

您好,我正在处理2个与外键相关的表,WorkOrderItemUsage和WorkOrderItemUsageLot。每个workOrderItemUsage可以有多个批次,因此是1对n的关系,为了简化工作,我实现了一个名为WorkOrderItemUsageWithLots的帮助程序实体,该实体用于使用room @Relation一次性检索两个表中的所有数据。

问题

我试图在单个列中使用@Relation,并且它可以正常工作,但是我需要基于诸如外键之类的复合键来稳定我的关系,我知道我不支持使用@Relation的复合键。我试图使用索引链接它,我是否丢失了某些内容?我是否必须拒绝使用@Relation来支持查询中的良好ol SQL连接?

  • 错误:在WorkOrderItemUsageWithLots中找不到父实体列woItemUsagePk
  • 错误会显示@Relation的可用列
  • 选项:woIdItemUsage,itemId,itemNumber,itemThirdNumber,itemDescription,quantityMeasure,branchId,branchDescription,locationId,doseQuantity,doseMeasureUnit,itemAction,itemPlague,itemJustification 私有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()}
    

1 个答案:

答案 0 :(得分:0)

您在实体类中没有声明woItemUsagePk字段。您需要声明一个字段来保存该值,Room不会为您动态声明它。