会议室数据库迁移无法正确处理ALTER TABLE迁移

Java.lang.IllegalStateException

迁移没有正确处理用户(therealandroid.github.com.roomcore.java.User)。

预期:

TableInfo {name =’user’,columns = {name = Column {name =’name’,type =’TEXT’,notNull = false,primaryKeyPosition = 0},age = Column {name =’age’,type =’INTEGER ‘,notNull = true,primaryKeyPosition = 0},id = Column {name =’id’,type =’INTEGER’,notNull = true,primaryKeyPosition = 1}},foreignKeys = []}find:

发现

TableInfo {name =’user’,columns = {name = Column {name =’name’,type =’TEXT’,notNull = false,primaryKeyPosition = 0},id = Column {name =’id’,type =’INTEGER ‘,notNull = true,primaryKeyPosition = 1},age = Column {name =’age’,type =’INTEGER’,notNull = false,primaryKeyPosition = 0}},foreignKeys = []}

我正在尝试执行一个简单的迁移,我有一个名为User的类,它有两列ID (primary key)NAME TEXT然后我用两个用户数据填充数据库,然后我在对象User添加列AGE在迁移常量中,我添加了一个alter table来添加这个新列,最后我将数据库1的版本替换为2。

这是代码

User.class

 @Entity(tableName = "user") public class User { @PrimaryKey private int id; @ColumnInfo(name = "name") private String name; @ColumnInfo(name = "age") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 

数据库类

 @Database(entities = {User.class}, version = 2) public abstract class RoomDatabaseImpl extends RoomDatabase { abstract UserDao userDao(); } 

迁移代码

 public static Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE 'user' ADD COLUMN 'age' INTEGER"); } }; 

它打来电话

 Room.databaseBuilder(context, RoomDatabaseImpl.class, "Sample.db") .addMigrations(MIGRATION_1_2) .allowMainThreadQueries() .build(); 

在更改添加AGE并执行迁移的对象之前,我添加了两个寄存器并且它可以工作。

执行迁移后,我只是尝试添加一个新用户:

  User user = new User(); user.setName("JoooJ"); user.setId(3); user.setAge(18); List userList = new ArrayList(); userList.add(user); App.database(this).userDao().insertAll(userList); // The crash happens here 

其他信息:

Android Studio 3和我没有在实际测试过。

依赖关系:

 compile "android.arch.persistence.room:runtime:1.0.0-alpha9-1" annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha9-1" compile "android.arch.persistence.room:rxjava2:1.0.0-alpha9-1" gradle 2.3.3 

有人可以帮助我,我真的不知道我做错了什么或者它是一个错误。

错误消息很难解析,但有一点不同:

TableInfo {name =’user’,columns = {name = Column {name =’name’,type =’TEXT’,notNull = false,primaryKeyPosition = 0},age = Column {name =’age’,type =’INTEGER ‘, notNull = true ,primaryKeyPosition = 0},id = Column {name =’id’,type =’INTEGER’,notNull = true,primaryKeyPosition = 1}},foreignKeys = []}find:

发现

TableInfo {name =’user’,columns = {name = Column {name =’name’,type =’TEXT’,notNull = false,primaryKeyPosition = 0},id = Column {name =’id’,type =’INTEGER ‘,notNull = true,primaryKeyPosition = 1},age = Column {name =’age’,type =’INTEGER’, notNull = false ,primaryKeyPosition = 0}},foreignKeys = []}

年龄可以为空,但是房间预计它不会为空。

将您的迁移更改为:

 database.execSQL("ALTER TABLE 'user' ADD COLUMN 'age' INTEGER NOT NULL"); 

由于此exception解释非常难以解析,因此我创建了一个小脚本 ,为您执行diff。

例:

 mig "java.lang.IllegalStateException: Migration failed. expected:TableInfo{name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, age=Column{name='age', type='INTEGER', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}}, foreignKeys=[]} , found:TableInfo{name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, age=Column{name='age', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[]}" 

结果:

预期/发现差异