Status Update
Comments
el...@google.com <el...@google.com> #2
as...@gmail.com <as...@gmail.com> #3
Here's my entity:
@Entity(tableName = "palette")
class ManualPaletteEntity(
@PrimaryKey @ColumnInfo(name = "id") val id: String,
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "time_millis") val timeMillis: Long,
)
@Entity(
tableName = "float_hsv",
foreignKeys = [
ForeignKey(
entity = ManualPaletteEntity::class,
parentColumns = ["id"],
childColumns = ["palette_id"],
onDelete = ForeignKey.CASCADE
)
]
)
data class FloatHSVEntity(
@ColumnInfo(name = "hue") val hue: Float,
@ColumnInfo(name = "saturation") val saturation: Float,
@ColumnInfo(name = "value") val value: Float,
@ColumnInfo(name = "alpha") val alpha: Float,
@ColumnInfo(name = "palette_id") val paletteId: String,
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int = 0,
)
yb...@google.com <yb...@google.com> #4
FK violation would happen based on the data in the database.
For that example, if you have anything in float_hsv
where palette_id
does not match an id
from ManuelPaletteEntity
, it would crash.
I'm also not sure sqlite is even able to detect that match after the fact, we should probably test that and figure out if we can workaround it.
as...@gmail.com <as...@gmail.com> #5
Alright, I'll try to figure out what's wrong
as...@gmail.com <as...@gmail.com> #6
Yep, you were right. There's a FK violation
yb...@google.com <yb...@google.com> #7
Thanks for checking it. @Elif, lets use this bug to see if we can improve the message.
We could even consider checking for FKey violations ourselves by generating a query and printing a better message. Though if that happens on production, that would mean repeatedly crashing the application :/.
That is probably the right thing to do. Room used to migrate destructively by default and developers didn't like it. We might also consider providing an API to delete violating entries in a future release.
el...@google.com <el...@google.com> #8
el...@google.com <el...@google.com> #9
as...@gmail.com <as...@gmail.com> #10
float_hsv
had a palette_id
that was deleted before. That's why I added the foreign key and onDelete = ForeignKey.CASCADE
to automatically delete float_hsv
when the palette was deleted.
ap...@google.com <ap...@google.com> #11
Branch: androidx-main
commit 7917c78c2e0212794a90e46fb6db554c90eec4b9
Author: Elif Bilgin <elifbilgin@google.com>
Date: Mon Jun 07 13:58:31 2021
Handle foreign key violations during Auto Migration more gracefully.
When doing a PRAGMA foreign key check after complex changes have been performed on the database, in the case of a foreign key violation, we are now outputting a clear error message notifying the user that a violation has been found, including the name of the relevant entity.
Bug: 190113935
Test: TransactionMethodProcessorTest.kt
Change-Id: I68e256074d2aa801bb8db9fced5e0099ff51b6df
M room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/1.json
M room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/2.json
A room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/3.json
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationDb.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationTest.java
M room/room-compiler/src/main/kotlin/androidx/room/writer/AutoMigrationWriter.kt
M room/room-runtime/api/restricted_current.txt
M room/room-runtime/src/main/java/androidx/room/util/DBUtil.java
Description
Component used: Room
Version used: 2.4.0-alpha02
I just added foreign keys on a couple of entities and got this error when trying to use AutoMigration.
Here's the generated code:
Stack trace: