我的实体
@Entity(tableName = "user_table")
data class User(
val userID:String,
val userName:String,
val userRoleName:String,
val adminID:String,
val name:String,
val email:String,
val phoneNumber:String,
val adminType:String,
val batchID:String,
val centerID:String,
val batchName:String,
val centerName:String,
val password:String
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
我添加了密码列;
我提供的迁移是
private val migration: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE user_table ADD COLUMN password TEXT")
}
}
我在这里用过
private fun buildDB(c: Context): MDatabase {
val dbName = "lms_database.db"
return Room.databaseBuilder(c.applicationContext, MDatabase::class.java, dbName)
.addMigrations(migration)
.build()
}
我遇到了一个很长的错误:
java.lang.IllegalStateException: Migration didn't properly handle: user_table(app.database.entities.User).
Expected:
TableInfo{name='user_table', columns={batchName=Column{name='batchName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerID=Column{name='centerID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminType=Column{name='adminType', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userName=Column{name='userName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, batchID=Column{name='batchID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userID=Column{name='userID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, password=Column{name='password', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, phoneNumber=Column{name='phoneNumber', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminID=Column{name='adminID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userRoleName=Column{name='userRoleName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerName=Column{name='centerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='user_table', columns={batchName=Column{name='batchName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerID=Column{name='centerID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminType=Column{name='adminType', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userName=Column{name='userName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, batchID=Column{name='batchID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userID=Column{name='userID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, password=Column{name='password', type='Text', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, phoneNumber=Column{name='phoneNumber', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminID=Column{name='adminID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userRoleName=Column{name='userRoleName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerName=Column{name='centerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:124)
数据库现在已经有一些数据。
答案 0 :(得分:2)
您必须在实体模型中设置密码nullable
或在迁移过程中提供非空值。尝试设置
任一
val password:String?
或
ALTER TABLE user_table ADD COLUMN password TEXT NOT NULL DEFAULT ''
N.B:用默认值替换为空