/**/

我要删除一对多关系中的所有值

父表:

@Entity(tableName = "Product")  
data class Products (  
  @PrimaryKey(autoGenerate = false)  
  @ColumnInfo(name = "id") var id : Int = 0,   
  @ColumnInfo(name = "name")
  var name  : String? = null,   
  @ColumnInfo(name = "category_id")
  var category_id : String? = null,  
  @ColumnInfo(name = "subcategory_id")
  var subcategory_id : String? = null,  
  @ColumnInfo(name = "other_images")
  var other_images: List<String>  = listOf(),  
  @ColumnInfo(name = "price")
  var price : String? = null,  
  @ColumnInfo(name = "variants")
  var variants : List<Variants> = listOf()  
)

子表:

@Entity(tableName = "Variant")  
data class Variants (  
  @PrimaryKey(autoGenerate = false)  
  @ColumnInfo(name = "id")
  var id : Int  = 0,  
  @ColumnInfo(name = "product_id")
  var product_id : String?  = null,  
  @ColumnInfo(name = "price")
  var price : String?  = null,   
  @ColumnInfo(name = "discounted_price")
  var discounted_price : String?  = null,   
  @ColumnInfo(name = "image")
  var image : String?  = null,   
  @ColumnInfo(name = "moq")
  var moq : String?  = null,   
  @ColumnInfo(name = "cart_count")
  var cart_count : String?  = null,   
  @ColumnInfo(name = "is_notify_me")
  var is_notify_me : Boolean? = null             
)

关系:

data class ProductWithVariants(  
    @Embedded val product: Products,   
    @Relation(   
        parentColumn = "id",   
        entityColumn = "id"    
    )    
    val variants: MutableList<Variants>   
)

简单地说...我想要delete all products and variantsDelete Single Product with corresponding variantsUpdate Single Product

关系有什么变化吗??我还需要在岛上查询!!

推荐答案

您应该使用ForeignKey来指定实体之间的关系.

正如文档所说:

外键允许您指定跨实体的约束,如 该SQLite将确保在您修改时关系有效 数据库.

要添加外键,请执行以下操作:

  1. 父类
@Entity
data class User(
    @PrimaryKey
    val userId: Int,
    val name: String
)
  1. children 班:
@Entity(
    foreignKeys = [
        ForeignKey(
            entity = User::class,
            parentColumns = arrayOf("userId"),
            childColumns = arrayOf("ownerUserId"),
            onDelete = ForeignKey.CASCADE
        )
    ]
)
data class Pet(
    @PrimaryKey
    val petId: Int,
    val name: String,
    @ColumnInfo(index = true)
    val ownerUserId: Int
)
  1. 定义对象之间的关系:
data class UserWithPets (
    @Embedded
    val user: User,
    @Relation(
        parentColumn = "userId",
        entityColumn = "ownerUserId"
    )
    val pets: List<Pet>
)
  1. 创建DAO:
@Dao
interface UserDao {
    @Insert
    suspend fun saveUser(user: User)

    @Insert
    suspend fun saveUsers(users: List<User>)

    @Insert
    suspend fun savePets(pets: List<Pet>)

    @Query("SELECT * FROM USER")
    suspend fun getUsers(): List<UserWithPets>

    @Query("DELETE FROM USER WHERE userId=:id")
    suspend fun deleteUser(id: Int)

    @Query("DELETE FROM USER")
    suspend fun deleteAllUsers()

    @Update
    suspend fun updatePet(pet: Pet)
}

Some example queries:

// Add new user
 val user = User(userId = 1, name = "User1")
 userDao.saveUser(user)

 var userWithPets = userDao.getUsers()
// result -> UserWithPets(user=User(userId=1, name=User1), pets=[])

// Add new pet
 val pet = Pet(petId = 1, ownerUserId = 1, name = "Pet Name")
 userDao.savePets(listOf(pet))

// Fetch users again
//UserWithPets(user=User(userId=1, name=User1), pets=[Pet(petId=1, name=Pet Name, ownerUserId=1)])

 
// Update pet
userDao.updatePet(pet.copy(name = "New Name"))

// Fetch users again
//UserWithPets(user=User(userId=1, name=User1), pets=[Pet(petId=1, name=New Name, ownerUserId=1)])

这样,无论何时删除User,它的所有Pets都将自动删除.

Android相关问答推荐

在 react native 中设置 react-native-paper 组件的样式

组成不重叠的元素

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

Jetpack Compose Arc 进度条动画(如何重启动画)

Android:appcompat 和 material 如何从默认创建 appcompat 和 material 视图?

自定义 Recyclerview [Android & Kotlin]

运行一次 kotlin 流,但在下游收到两次

Android 应用程序在启动时自动启动

如何在 kotlin 的 android room DB 中设置一对多关系

如果我在网络请求中指定它们是否与判断网络功能相关

如何使在库范围之外无法访问的接口的具体实现.?

如何将 Android Studio 与模拟器连接起来

约束布局垂直对齐中心

从视图中删除所有子视图

如何从 URI 获取完整的文件路径

片段中未调用 OnCreateOptionsMenu()

通过 WhatsApp 发送消息

如何判断edittext的文本是否为Electron邮件地址?

android 片段 onRestoreInstanceState

Android:复选框监听器