I this class:
data class UserEntity(
@PrimaryKey var uid: String,
var name: String?,
var photo: String?,
var status: Int = 0,
var interaction: Long? = null,
var additional: String? = ""
)
When trying to run migration:
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE UserEntity ADD COLUMN additional TEXT NOT NULL DEFAULT ''")
}
}
I get:
Caused by: java.lang.IllegalStateException: Migration didn't properly handle: UserEntity(com.michlindevelopment.way.mainapp.database.UserEntity).
Expected:
TableInfo{name='UserEntity', columns={name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, interaction=Column{name='interaction', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, uid=Column{name='uid', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='undefined'}, photo=Column{name='photo', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='undefined'}, additional=Column{name='additional', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='UserEntity', columns={uid=Column{name='uid', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='undefined'}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, photo=Column{name='photo', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='undefined'}, interaction=Column{name='interaction', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, additional=Column{name='additional', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue=''''}}, foreignKeys=[], indices=[]}
There is problem with the order maybe? I expected the name column is first, in found uid is first
The order of the columns is not an issue.
There are issues with the new column as per the expected is :-
additional=Column{name='additional', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}}
and the found is
additional=Column{name='additional', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue=''''
i.e. Room (according to the @Entity
definition) is that:-
NOT NULL
i.e. nulls are allowed as per var additional: String? = ""
i.e. the ?
says that the value is nullable.DEFAULT
, whilst the DEFAULT
value is an empty String as per DEFAULT ''
and thus defaultValue=''''
To fix the issues above you should use:-
ALTER TABLE UserEntity ADD COLUMN additional TEXT
i.e. remove the NOT NULL and have no DEFAULT and thus the default will be null.
However, the easier way to avoid issues with Room's expectations is to let Room tell you exactly what it expects, as it creates the SQL used to create the tables whenever you compile the project if the entities parameter of the @Database
annotation includes the @Entity
annotated class.
So to get the exact column definition, you:-
@Database
annotated class but suffixed with _Impl
.createAllTables
.Note if you need a DEFAULT value then you need to specify this in the defaultValue
parameter of the @ColumnInfo
annotation (otherwise the expected but found issue). Furthermore if the column has the NOT NULL constraint then a default value is required as it cannot be null as per
If a NOT NULL constraint is specified, then the column must have a default value other than NULL.