问题
I am testing a room migration and on the "migration and validation" step I got an java.lang.IllegalStateException: Migration failed. due to a wrong primaryKeyPosition
value on the "service_id" column. Digging a little bit deeper I noticed that on the Expected side the attributes in the actual_visit_id_service_id_primary_key have different pk indexes (1, 2), while on the Found side, both have the same (1, 1)
I checked into the database schema of table actual_visit_service in the installed app and the pk for the column service_id is 2 (like expected)
The failing entity:
@Entity(
tableName = ACTUAL_VISIT_SERVICE_TABLE,
indices = [Index(
value = [ACTUAL_VISIT_SERVICE_SERVICE_ID, ACTUAL_VISIT_SERVICE_ACTUAL_VISIT_ID], unique = true
)],
foreignKeys = [(ForeignKey(
entity = ActualVisitEntity::class,
parentColumns = [ID],
childColumns = [ACTUAL_VISIT_SERVICE_ACTUAL_VISIT_ID],
onDelete = ForeignKey.CASCADE
))],
primaryKeys = [ACTUAL_VISIT_SERVICE_ACTUAL_VISIT_ID, ACTUAL_VISIT_SERVICE_SERVICE_ID]
)
data class ActualVisitServicesEntity(
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_ACTUAL_VISIT_ID)
val actualVisitId: String,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_AMOUNT)
val amount: Double?,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_DURATION)
val duration: Int?,
@ColumnInfo(name = IS_SYNCHRONIZED)
var isSynchronized: Boolean = false,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_HAS_BEEN_PROVIDED)
var hasBeenProvided: Boolean,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_REJECTION_CATEGORY)
var rejectionCategory: String?,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_REJECTION_REASON)
var rejectionReason: String?,
@ColumnInfo(name = ACTUAL_VISIT_SERVICE_SERVICE_ID)
val serviceId: String
) : BaseEntity() {
....
}
The migration:
@JvmField
val MIGRATION_18_TO_19 = object : Migration(18, 19) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE planned_visit ADD COLUMN hint TEXT")
}
}
The error I got:
java.lang.IllegalStateException: Migration failed.
Expected:TableInfo{name='actual_visit_services', columns={duration=Column{name='duration', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, amount=Column{name='amount', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0}, actual_visit_id=Column{name='actual_visit_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, rejection_category=Column{name='rejection_category', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, has_been_provided=Column{name='has_been_provided', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, service_id=Column{name='service_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=2}, rejection_reason=Column{name='rejection_reason', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_synchronized=Column{name='is_synchronized', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[ForeignKey{referenceTable='actual_visits', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[actual_visit_id], referenceColumnNames=[id]}], indices=[Index{name='index_actual_visit_services_service_id_actual_visit_id', unique=true, columns=[service_id, actual_visit_id]}]}
found:TableInfo{name='actual_visit_services', columns={duration=Column{name='duration', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, amount=Column{name='amount', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0}, actual_visit_id=Column{name='actual_visit_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, rejection_category=Column{name='rejection_category', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, has_been_provided=Column{name='has_been_provided', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, service_id=Column{name='service_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, rejection_reason=Column{name='rejection_reason', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_synchronized=Column{name='is_synchronized', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[ForeignKey{referenceTable='actual_visits', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[actual_visit_id], referenceColumnNames=[id]}], indices=null}
As you see on "Expected" you have:
service_id=Column{name= ... primaryKeyPosition=2}
but on "Found":
service_id=Column{name= ... primaryKeyPosition=1}
I use Room from the beginning but I just started with the migrations, being the previous database build version 18 and the new one 19. I run the regular entities and DAOs test with Robolectric
and I intend to do the same with the migration tests.
Why do the two attributes in the composed primary key have the same pk index in the migrated database?
来源:https://stackoverflow.com/questions/57875012/error-on-room-database-migration-test-due-to-a-wrong-primarykeyposition-value