+-
android – 房间数据库迁移不能正确处理ALTER TABLE迁移

Java.lang.IllegalStateException

Migration didn’t properly handle
user(therealandroid.github.com.roomcore.java.User).

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:

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=[]}

我正在尝试执行一个简单的迁移,我有一个名为Userand的类,它有两列ID(主键)和NAME TEXT,然后我用两个用户数据填充数据库,然后我在对象User和in中添加列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<User> 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=[]} 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=[]}

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

将您的迁移更改为:

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

由于此异常解释非常难以解析,因此我创建了a small script为您做差异.

例:

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=[]}"

结果:

expected/found diff

点击查看更多相关文章

转载注明原文:android – 房间数据库迁移不能正确处理ALTER TABLE迁移 - 乐贴网