在会议室中使用外键从另一个实体获取列

时间:2019-03-23 00:27:50

标签: android kotlin android-room

我有两个不同的实体。一个具有对另一个的两个引用,我需要获取该引用的属性。

<?php if( isset( $_GET['reason'] ) ){ $reason = $_GET['reason']; echo "Login error: " . $reason; } ?> my_main_table.primary_type的外键,而types._idmy_main_table.secondary_type的外键,可以为空。

是使用types._id库复制的预填充数据库,因此该方案已在数据库中完成。这是数据库的图:

database diagram

这是我的主要实体:

RoomAsset

这是我的参考资料:

@Entity(
    tableName = "my_main_table",
    foreignKeys = [
        ForeignKey(
            entity = Type::class,
            parentColumns = ["_id"],
            childColumns = ["secondary_type"],
            onDelete = ForeignKey.RESTRICT,
            onUpdate = ForeignKey.RESTRICT
        ),
        ForeignKey(
            entity = Type::class,
            parentColumns = ["_id"],
            childColumns = ["primary_type"],
            onDelete = ForeignKey.RESTRICT,
            onUpdate = ForeignKey.RESTRICT
        )
    ]
)
data class MainTable(
    @PrimaryKey
    @ColumnInfo(name = "_id", index = true)
    val id: Int,

    @ColumnInfo(name = "number")
    val number: String,

    @ColumnInfo(name = "name")
    val name: String,

    @ColumnInfo(name = "primary_type")
    val primaryType: String,

    @ColumnInfo(name = "secondary_type")
    val secondaryType: String?
)

最后是@Entity(tableName = "types") data class Type( @PrimaryKey @ColumnInfo(name = "_id") val id: Int, @ColumnInfo(name = "name") val name: String ) 的SQL代码:

@Query

我想要的是通过关系获得SELECT p._id AS _id, p.number AS number, p.name AS name, pt.name AS primary_type, st.name AS secondary_type FROM my_main_table p INNER JOIN types pt ON p.primary_type == pt._id LEFT JOIN types st ON p.secondary_type == st._id 的值。但是我不知道怎么做。我是否需要在存储库中使用其他方法来获取名称的值?

谢谢。

0 个答案:

没有答案