【问题标题】:Ambigous column in POJO result query room androidPOJO结果查询室android中的歧义列
【发布时间】:2020-11-12 00:37:01
【问题描述】:

我有两个表 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 我怎么能得到这样的结果呢

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

【问题讨论】:

    标签: android sqlite kotlin android-room pojo


    【解决方案1】:

    问题解决了。问题是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>
    

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2013-09-26
      相关资源
      最近更新 更多