简体   繁体   English

POJO结果查询室android中的歧义列

[英]Ambigous column in POJO result query room android

I have two tables sites and groups with the data classes like that我有两个表sitesgroups其中包含这样的数据类

@Entity(tableName = "sites")
data class Site(

    @PrimaryKey
    @ColumnInfo(name = "site_id")
    val siteId: Int,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String

)

@Entity(tableName = "groups")
data class Group(

    @PrimaryKey
    @ColumnInfo(name = "group_id")
    var groupId: Int,

    @ColumnInfo(name = "site_id")
    val siteId: Int,

    @ColumnInfo(name = "description", defaultValue = "")
    val description: String
)

so as we can see each Site has a list of Groups.所以我们可以看到每个站点都有一个组列表。

What i want is, given a site_id and group_id how can i get a result pojo like that我想要的是,给定一个site_idgroup_id ,我怎么能得到这样的结果 pojo

class SiteGroup {
    
    @Embedded
    var site: Site? = null
    
    @Relation(parentColumn = "site_id", entityColumn = "site_id", entity = Group::class)
    var groups: Group? = null
    
}

I have tried the below我已经尝试过以下

@Query("""Select * from sites
        inner join groups on groups.site_id = :siteId
        where site_id = :siteId and groups.group_id = :groupId
    """)
    fun getByIdWithGroup(siteId: Int, groupId: Int): LiveData<SiteGroup>

but i get the following exception on build time但我在构建时收到以下异常

error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (ambiguous column name: site_id)
    public abstract androidx.lifecycle.LiveData<com.example.model.entities.pojos.SiteGroup> getByIdWithGroups(int siteId, int groupId) 
Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

Problem solved.问题解决了。 The problem was the where site_id .问题是在where site_id The correct way is正确的方法是

@Query("""Select * from sites
        inner join groups on groups.site_id = :siteId
        where sites.site_id = :siteId and groups.group_id = :groupId
    """)
    fun getByIdWithGroup(siteId: Int, groupId: Int): LiveData<SiteGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM