我有以下表格:
@Entity(
tableName = "users"
)
class Users {
@PrimaryKey(autoGenerate = true)
var id: Long? = null
@NonNull
var name: String? = null
}
@Entity(
tableName = "pets",
foreignKeys = [
ForeignKey(
entity = Users::class,
parentColumns = ["id"],
childColumns = ["owner_id"],
onDelete = ForeignKey.CASCADE
)
]
)
class Pets {
@PrimaryKey(autoGenerate = true)
var id: Long? = null
@NonNull
var name: String? = null
@ColumnInfo(name = "owner_id")
var ownerId: Long? = null
}
当我运行删除所有用户表行的迁移时,pets表不受影响。这些行不会自动删除。
object Migration_1_2 : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("""
DELETE FROM users
""")
}
}
即使在迁移之前执行以下代码片段,它也不起作用。
database.execSQL("PRAGMA foreign_keys=ON;");
我该怎么做才能让
onDelete = ForeignKey.CASCADE
工作