簡體   English   中英

POJO結果查詢室android中的歧義列

[英]Ambigous column in POJO result query room android

我有兩個表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
)

所以我們可以看到每個站點都有一個組列表。

我想要的是,給定一個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
    
}

我已經嘗試過以下

@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>

但我在構建時收到以下異常

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).

問題解決了。 問題是在where site_id 正確的方法是

@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