How to join multiple instances of same object in Android Room?

前端 未结 3 691
北恋
北恋 2020-12-21 01:28

NOTE: I cannot use relation as we had performance issue which is not reproduced on direct join query.

Until I added target user and from group use

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 01:52

    In the query, you have to set an alias for the table chat_user as you are joining it twice in the same name which will confuse database engine so it tells you that the field is ambiguous.

    EDIT:

    I found something that may be similar to your issue: https://github.com/JetBrains/Exposed/issues/177

    Also they referred to this code as an example to fix this issue:

        object Users : Table() {
            val id = varchar("id", 10).primaryKey()
            val name = varchar("name", length = 50)
            val residentialCityId = optReference("resid_city_id", Cities)
            val bornCityId = optReference("born_city_id", Cities)
        }
    
        object Cities : IntIdTable() {
            val name = varchar("name", 50) // Column
        }
        fun test() {
            val userTable1 = Users.alias("u1")
            val userTable2 = Users.alias("u2")
    
            Cities
                .innerJoin(userTable1, {Cities.id}, {userTable1[Users.residentialCityId]})
                .innerJoin(userTable2, {Cities.id}, {userTable2[Users.bornCityId]})
                .selectAll()
        }
    

提交回复
热议问题