会议室数据库迁移失败:ALTER TABLE添加多个列

Rav*_*wal 9 sqlite android-sqlite android-room

我通过提供从3到4的迁移将数据库从版本3升级到版本4。

这是我的迁移代码:

private static Migration MIGRATION_3_4 = new Migration(3, 4) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
      database.execSQL("ALTER TABLE caption_table ADD COLUMN localVideoUrl TEXT;");
      database.execSQL("ALTER TABLE caption_table ADD COLUMN postType TEXT");
      database.execSQL("ALTER TABLE caption_table ADD COLUMN videoUrl TEXT");
    }
};
Run Code Online (Sandbox Code Playgroud)

这是创建会议室数据库的代码

this.mAppDataBase = Room.databaseBuilder(getApplicationContext(), AppDataBase.class, "my_db")
                        .addMigrations(MIGRATION_2_3, MIGRATION_3_4)
                        .build();
Run Code Online (Sandbox Code Playgroud)

这是我在PostModel上添加的代码

@Expose
private String postType;

@Expose
private String videoUrl;

@Expose
private String localVideoUrl;

public String getPostType() {
    return postType;
}

public void setPostType(String postType) {
    this.postType = postType;
}

public String getVideoUrl() {
    return videoUrl;
}

public void setVideoUrl(String videoUrl) {
    this.videoUrl = videoUrl;
}

public String getLocalVideoUrl() {
    return localVideoUrl;
}

public void setLocalVideoUrl(String localVideoUrl) {
    this.localVideoUrl = localVideoUrl;
}
Run Code Online (Sandbox Code Playgroud)

下面是我遇到的错误。该错误与房间实体的notNull属性无关。

java.lang.IllegalStateException:迁移未正确处理帖子(com.myapp.Database.PostModel)。

预期:TableInfo {name ='posts',columns = {imageWidth = Column {name ='imageWidth',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0},localVideoUrl = Column {name = 'localVideoUrl',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageLocalUrl = Column {name ='authorImageLocalUrl',type ='TEXT',affinity ='2',notNull = false ,primaryKeyPosition = 0},videoUrl = Column {name ='videoUrl',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},imageLocalUrl = Column {name ='imageLocalUrl',type =' TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},postType = Column {name ='postType',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorName = Column {名称='authorName',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageUrl = Column {name ='imageUrl',类型='TEXT',亲和力=' 2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title = Column {name =' title',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {name ='authorImageUrl',type ='TEXT',affinity ='2',notNull = false, primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],indexs = []}亲和力='2',notNull = false,primaryKeyPosition = 0},imageUrl = Column {name ='imageUrl',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},id = Column { name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title = Column {name ='title',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {名称='authorImageUrl',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',类型='INTEGER',亲和力='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],索引= []}亲和力='2',notNull = false,primaryKeyPosition = 0},imageUrl = Column {name ='imageUrl',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},id = Column { name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title = Column {name ='title',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {名称='authorImageUrl',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',类型='INTEGER',亲和力='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],索引= []}2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title = Column {name =' title',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {name ='authorImageUrl',type ='TEXT',affinity ='2',notNull = false, primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],indexs = []}2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title = Column {name =' title',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {name ='authorImageUrl',type ='TEXT',affinity ='2',notNull = false, primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],indexs = []}authorImageUrl =列{name ='authorImageUrl',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',亲和力=' 3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],索引= []}authorImageUrl =列{name ='authorImageUrl',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',亲和力=' 3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],索引= []}

找到:TableInfo {名称='帖子',列= {图像宽度=列{名称='图像宽度',类型='INTEGER',亲和力='3',notNull = true,primaryKeyPosition = 0},authorImageLocalUrl = Column {name = 'authorImageLocalUrl',类型='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},imageLocalUrl = Column {name ='imageLocalUrl',类型='TEXT',亲和力='2',notNull = false ,primaryKeyPosition = 0},authorName = Column {name ='authorName',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},imageUrl = Column {name ='imageUrl',type =' TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1},title =列{name ='title',type ='TEXT',亲和力='2',notNull = false,primaryKeyPosition = 0},authorImageUrl = Column {name ='authorImageUrl',type ='TEXT',亲和力=' 2',notNull = false,primaryKeyPosition = 0},imageHeight = Column {name ='imageHeight',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 0}},foreignKeys = [],索引= []}

dgl*_*ano 5

比较日志中的 Expected 与 Found JSON,很明显,发生错误是因为找到的表没有您打算添加的 3 个新列:postTypevideoUrllocalVideoUrl(您可以使用此脚本来比较日志中的两个 JSON 对象)

问题可能是在您的迁移代码中,您将新列添加到名为 的表中caption_table,而根据日志,失败的表名为posts。尝试调整迁移代码以将新列添加到正确的表中。