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

后端 未结 3 1946

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:45

    I solved that problem by adding prefix to column names so they are unique. If your case does not allow changing column names you should use some of the provided answers and @Embedded(prefix) annotation.

    @Entity(tableName = "animal")
    public class Animal {
        @PrimaryKey
        @ColumnInfo(name = "animal_id")
        public long id;
        ...
    }
    
    @Entity(tableName = "animal_type")
    public class AnimalType {
        @PrimaryKey
        @ColumnInfo(name = "animal_type_id")
        public long id;
        ...
    }
    

    And your query will be:

    SELECT * FROM animal JOIN animal_type ON animal_id = animal_type_id
    

提交回复
热议问题