会议室数据库迁移无法正确处理ALTER TABLE迁移 [英] Room Database Migration doesnt properly handle ALTER TABLE migration

查看:76
本文介绍了会议室数据库迁移无法正确处理ALTER TABLE迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java.lang.IllegalStateException

Java.lang.IllegalStateException

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

Migration didn't properly handle user(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 = []}找到了:

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

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和迁移常量中,添加alter table来添加此新列,最后我将数据库的版本1替换为2.

I'm trying to perform a simple migration, I have a class called Userand it have two columns ID (primary key) and NAME TEXT and then I populate database with two users data, then I add the column AGE in the object User and in the Migration constant I add an alter table to add this new column and lastly I replace version of the database 1 to 2.

这是代码

User.class

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并执行迁移,然后添加两个寄存器即可.

Before change the object adding AGE and performing the migration I add two register and it works.

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

After performing the migration, I just tried to add a new User as bellow:

  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,我没有经过实际测试.

Android Studio 3 and I didn't tested in the actual.

依赖项:

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

有人可以帮我吗,我真的不知道我做错了什么,或者这是一个错误.

Can someone help me please, I realy don't know what im doing wrong or if it is a bug.

推荐答案

该错误消息很难解析,但是有一个区别:

The error message is hard to parse, but there's a difference:

TableInfo {name ='user',列= {name = Column {name ='name',type ='TEXT',notNull = false,primaryKeyPosition = 0},age = Column {name ='age',类型='INTEGER', notNull = true ,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',notNull = true,primaryKeyPosition = 1}},foreignKeys = []}找到了:

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',列= {name = Column {name ='name',type ='TEXT',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',类型='INTEGER',notNull = true,primaryKeyPosition = 1},age = Column {name ='age',type ='INTEGER', notNull = false ,primaryKeyPosition = 0}},foreignKeys = []}

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

年龄是可以为空的,但Room希望它不为空.

Age is nullable but Room expected it to be not null.

将您的迁移更改为:

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


由于此异常说明很难解析,因此我创建了一个小脚本您的差异.


Since this exception explanation is VERY difficult to parse, I have created a small script that does the diff for you.

示例:

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

结果:

这篇关于会议室数据库迁移无法正确处理ALTER TABLE迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆