kotlin android 领域数据库创建外键(链接对象)

kotlin android Realm DB create a foreign key (Linking Objects)

我有 2 table 个学生和一个老师。

学生 table 有 3 个字段,姓名,roll_no,科目。 Roll_no 是主键。

另一个 table 命名标记有 4 个字段主题 ID、主题名称、主题分数和 roll_no roll_no 字段将是外键。

如何创建关系,如标记 table 指的是学生 table 的 roll_no.

的外键

简答:领域没有外键。

长答案:Realm 希望您将数据视为对象,而不是表格和链接 (https://realm.io/docs/kotlin/latest/#relationships)。

假设 roll_no 有点像 student_id - 您可以像这样对数据建模:

open class Student(
    @PrimaryKey
    var id: String = "",
    var markedSubjects: RealmList<MarkedSubject> = RealmList()
): RealmObject()

open class MarkedSubject(
    var subject: Subject? = null,
    var mark: Int? = null
): RealmObject()