gpt4 book ai didi

Android Room级联删除一对多关系

转载 作者:行者123 更新时间:2023-12-04 23:50:57 24 4
gpt4 key购买 nike

假设我有两个实体,锻炼和锻炼,锻炼(一个)和锻炼(许多)之间存在一对多的关系。实体是这样设置的

锻炼实体:

@Entity(
tableName = "workouts",
indices = [Index("startDate")]
)
data class Workout(
@PrimaryKey
val startDate: String,

val workoutName: String
)

行使实体:
@Entity
data class Exercise(
@PrimaryKey(autoGenerate = true)
val exerciseId: Long = 0,

val workoutId: String,

val name: String
)

锻炼锻炼:
@Entity(
foreignKeys = [ForeignKey(
entity = Workout::class,
parentColumns = arrayOf("startDate"),
childColumns = arrayOf("workoutId"),
onDelete = ForeignKey.CASCADE
)]
)
data class Exercise(
@PrimaryKey(autoGenerate = true)
val exerciseId: Long = 0,

val workoutId: String,

val name: String
)

这就是我获得与锻炼相关的练习的方式:
@Transaction
@Query("SELECT * FROM workouts WHERE startDate = :startDate")
suspend fun getWorkoutWithExercises(startDate: String): WorkoutWithExercises

所以我的问题是,如果包含练习的锻炼实例被删除,相关的练习也会被删除吗?如果没有,这将如何实现?

谢谢

最佳答案

当您创建了 Foreign Key 时,这些练习也将被删除。为表练习。

关于Android Room级联删除一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61217367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com