我正在努力实现以下目标:

enter image description here

我正在和100一起工作.

In code:

@Entity(tableName = "BookReadingSchedule")
data class BookReadingSchedule(
    @PrimaryKey
    @ColumnInfo(name = "schedule_id")
    val id: Long = 0,
    val name: String = ""
)

@Entity(tableName = "Books")
data class Books(
    @PrimaryKey
    @ColumnInfo(name = "book_id")
    val id: Long = 0,
    val title: String = ""
)

@Entity(
    tableName = "BooksToRead"
    foreignKeys = [
        ForeignKey(
            entity = BookReadingSchedule::class,
            parentColumns = ["BookReadingSchedule_id"],
            childColumns = ["to_book_id"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    ]
)
data class BooksToRead(
    @PrimaryKey
    @ColumnInfo(name = "id")
    val id: Long = 0,
    val title: String = ""
    @ColumnInfo(name = "to_book_id", index = true)
    val book_id: Long,
)

试图建立这种关系,但是,我不确定这是不是完全正确.我在微软上用过LINQ C#,但从来没有在Android上做过这么多.我在利用柯特林.

我选中的链接(before I posted here):


EDIT:,Idea is that for every schedule, there will be only a handful of books that were select to be included within that schedule..

推荐答案

对于相应的表,您的@ColumnInfo(name = "schedule_id")应该是parentColum.所以你需要这样的东西

@Entity(
    tableName = "BooksToRead"
    foreignKeys = [
        ForeignKey(
            entity = BookReadingSchedule::class,
            parentColumns = ["schedule_id"],
            childColumns = ["to_schedule_id"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        ),
        ForeignKey(
            entity = Books::class,
            parentColumns = ["book_id"],
            childColumns = ["to_book_id"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    ]
)

Android相关问答推荐

在Android应用程序上使用Room数据库时,是否可以预先填充数据

如何使用Gradle风味在两个Kotlin导入(Google vs Amazon Java billing library)之间进行 Select ?

了解数据加载在Kotlin中的工作原理

Android开发:主题排版不适用于按钮文本

在模块中找到重复的类com.google.Firebase.auth.ktx.AuthKt||Android Studio

在Jetpack Compose中,如何判断屏幕是否已重新组合?

DocumentFile.canWrite()、DocumentFile.Existes()-使用本地内置手机存储(而不是云)时性能较差(占用太多CPU时间)

没有打开历史记录的活动意向 Select 器完成调用活动

Android 构建失败:找不到 flexbox2.0.1.aar

Android 应用程序从 Android Studio 安装,但不是作为 .apk 在外部安装.抛出java.lang.UnsatisfiedLinkError

如何删除 Jetpack Compose 中按钮的左边框?

为什么@PrimaryKey val id: Int? = null 在创建 Room 实体时有效吗?

修复错误 Invariant Violation: requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager

如何在 Jetpack Compose 中向图像视图添加对角色带?

如何在 JetpackCompose 的 LazyColumn 中 Select 多个项目

观察软键盘可见性,打开/关闭 Jetpack Compose

缺少类 com.google.android.datatransport.runtime.ForcedSender

单击登录按钮后从应用程序中退出

为什么使用 React Native 和 expo 创建的 APK 体积这么大?

我可以在不解密的情况下使用 JSch 获取加密的 SSH 私钥的类型或 fingerprint 吗?