【问题标题】:Android Room and nested relationships errorAndroid Room 和嵌套关系错误
【发布时间】:2020-12-17 18:50:51
【问题描述】:

我有以下 3 个实体:

@Entity(tableName = "PROPERTY",
    indices = [
        androidx.room.Index(value = ["id"], unique = true)
    ])
data class Property (
    @Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
    @SerializedName("type") @ColumnInfo(name = "type") val type: ItemPropertyType,
    @SerializedName("code") @ColumnInfo(name = "code") val code: String,
    @SerializedName("default_description") @ColumnInfo(name = "default_description") val defaultDescription: String?,
    @SerializedName("description_id") @ColumnInfo(name = "description_id") val descriptionId: UUID?,
    @SerializedName("default_property") @ColumnInfo(name = "default_property") val defaultProperty: Boolean,
    @SerializedName("entity") @ColumnInfo(name = "entity") val entity: String = "ITEM"
)

@Entity(tableName = "PROPERTY_SET_DETAIL",
    indices = [
        Index(value = ["id"], unique = true),
        Index(value = ["property_id"], unique = false)
    ],
    foreignKeys = [
        ForeignKey(entity = PropertySet::class, parentColumns = ["id"], childColumns = ["property_set_id"]),
        ForeignKey(entity = Property::class, parentColumns = ["id"], childColumns = ["property_id"])])
data class PropertySetDetail (
    @Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
    @SerializedName("property_id") @ColumnInfo(name = "property_id") val propertyId: UUID,
    @SerializedName("value") @ColumnInfo(name = "value") var value: String?,
    @SerializedName("rank") @ColumnInfo(name = "rank") val rank: Int,
    @SerializedName("property_set_id") @ColumnInfo(name = "property_set_id") val propertySetId: UUID
)

@Entity(tableName = "PROPERTY_SET",
    indices = [Index(value = ["id"], unique = true), Index(value = ["properties_charset"], unique = false), Index(value = ["hashkey"], unique = false)])
data class PropertySet (
    @Expose @PrimaryKey override val id: UUID = UUID.randomUUID(),
    @SerializedName("item_id") @ColumnInfo(name = "item_id") val itemId: UUID,
    @SerializedName("properties_charset") @ColumnInfo(name = "properties_charset") var propertiesCharset: String?,
    @SerializedName("hashkey") @ColumnInfo(name = "hashkey") var hashkey: String?,
)

以及上面声明的 3 个实体之间的嵌套关系所需的以下数据类:

data class PropertySetAndDetails(
    @Embedded
    val set: PropertySet,

    @Relation(
        entity = PropertySetDetail::class,
        parentColumn = "id",
        entityColumn = "property_set_id"
    ) val details: List<PropertyDetailAndProperty>,
) 

data class PropertyDetailAndProperty(
    @Embedded
    val property: Property,

    @Relation(
        parentColumn = "id",
        entityColumn = "property_id"
    ) val detail: PropertySetDetail

)

以及如下DAO查询函数:

@Transaction
@Query("select * from property_set where item_id = :itemId")
suspend fun getSetsForItem(itemId: UUID): List<PropertySetAndDetails>

没有办法让它工作。我收到以下构建错误:

错误:查询有问题:[SQLITE_ERROR] SQL 错误或 缺少数据库(没有这样的列:类型)

我还为

声明了以下转换函数

项目属性类型:

@TypeConverter
    fun toItemPropertyType(v: Byte?): ItemPropertyType {
        return when (v) {
            null -> ItemPropertyType.Unknown
            else -> ItemPropertyType.getByValue(v)
        }
    }

    @TypeConverter
    fun fromItemPropertyType(t: ItemPropertyType?): Byte {
        return when (t) {
            null -> ItemPropertyType.Unknown.value
            else -> t.value
        }
    }

有人能解释一下为什么这种嵌套关系不起作用吗?提前致谢。

【问题讨论】:

    标签: android kotlin relationship android-room


    【解决方案1】:

    谁能解释一下为什么这种嵌套关系不起作用?

    我不能(我们称它为 Room 的魔法),但我可以提示您如何使这项工作发挥作用。尝试将您的 PropertyDetailAndProperty 类更改为:

    data class PropertyDetailAndProperty(
        @Embedded
        val detail: PropertySetDetail,
    
        @Relation(
            parentColumn = "property_id",
            entityColumn = "id"
        ) val property: Property
    )
    

    似乎嵌套类应该包含您在外部类中使用的实体(在您的情况下为PropertySetDetail),而不是@Relation。为什么?好吧,我猜这只是开发人员的设计。

    【讨论】:

    • 非常感谢!奇怪的是我也尝试过(但没有清除缓存并且它不起作用。比我清除缓存并从那时起工作顺利。如果从 Room 插件中获得更有意义的编译错误会很好 -在...这将节省几个小时
    【解决方案2】:

    您是否尝试过使用转换器? Room 无法识别此数据(“ItemPropertyType”)。这就是您收到此错误的原因。

    Doc

    【讨论】:

    • 感谢您的及时回复,但不幸的是,我做到了。我还在最后用转换器的声明更新了帖子。还有其他想法吗?
    • 我也尝试将类型的类型更改为 Int 但我得到了同样的错误。如果我将类型移动到另一个位置(第二列而不是第一列),我会在新的第一列中收到错误,例如:代码
    • 我在想。如果我有新想法,我会更新此评论。
    • 好的,tx,只是为了添加细节。看起来它无法将表属性的值绑定到结果集。至少看起来是这样的
    • 为什么PROPERTY SET表中没有“type”值?
    猜你喜欢
    • 1970-01-01
    • 2018-03-05
    • 2018-02-18
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2017-11-23
    • 2021-08-05
    • 2017-10-19
    相关资源
    最近更新 更多