【发布时间】:2020-12-17 00:27:01
【问题描述】:
@Entity
data class Product (
@PrimaryKey(autoGenerate = true)
var id: Long? = null
)
data class ProductList (
@Embedded var products: Product,
@Relation(
parentColumn = "id",
entityColumn = "productId",
entity = GroceryItem::class
)
var courses: List<GroceryItem?>? = null
)
@Entity
data class GroceryItem (
@PrimaryKey
var id: Int? = null,
var image: String? = null,
var price: String?= null
)
这里我没有任何常见的字段,所以我如何关联这两个表或如何将房间自动生成的 id 添加为外键
【问题讨论】:
-
this 有帮助吗?
-
不,就我而言,我没有任何公共字段@stachu
-
好的,如果没有公共字段,那么外键应该如何工作?在纯 SQL 中,我们暂时跳过 Room
-
我不明白你所说的在第二个表中添加一个表字段是什么意思。您可以做的是在第二个实体中创建一个
productid字段,并将其限制为仅从product[id]获取值,基本上是经典的foreign key -
你需要在
GroceryItem中创建一个名为productId的新属性,dao 只会使用实体中定义的对象
标签: java android sql database android-room