Android Room: How to read from two tables at the same time - duplicate id column

后端 未结 3 1949

I\'m new to Android Room. I want to read from a table and also read from a related table. The relationship is pretty common. One table defines instances. The other table defines

3条回答
  •  长发绾君心
    2021-02-07 09:40

    Instead of using * to collect all the fields, you need to specifically provide column names because id is found in both tables hence causing the error. Change your query to something like:

    select animal.id AS 'ID', animal.columnA, animal.columnB,
    animal_type.columnA, animal_type.columnB from animal
    join animal_type on (animal.id = animal_type.id)
    

    You dont need to fetch IDs from both tables since they will be same, so one is enough. However, replace coulmnA, columnB, etc. with the valid column names you want to fetch.

提交回复
热议问题