房间迁移变更表未添加新列&一次又一次地迁移 [英] Room Migration Alter Table not adding new column & migrate getting called again and again

查看:63
本文介绍了房间迁移变更表未添加新列&一次又一次地迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我正在使用会议室并尝试添加从数据库版本1到2的迁移,但是我的alter命令不起作用 我当前的实现如下:

So basically i am using room and trying to add migration from database version 1 to 2 but my alter command is not working My current implementation is below :

 void init() {
    db = Room.databaseBuilder(Global.getInstance(),
            AppDatabase.class, "feed").addMigrations(MIGRATION_1_2).build();
}

迁移属性:

static final Migration MIGRATION_1_2 = new Migration(1,2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {


        database.execSQL("ALTER TABLE 'post' ADD COLUMN 'age' INTEGER NOT NULL DEFAULT 0");
        Log.d("VROM","Migration");
    }
};

数据库实现:

@Database(entities = {Feed.class, DownloadModel.class}, version = 1) public abstract class AppDatabase extends RoomDatabase {
public abstract DaoAccess getFeedDao();}

因此,将版本从1递增到2后,将执行execSQL(),但不会在我的数据库中添加新列. 我已经从应用程序目录中拉了我的数据库,并检查了多次,但列不存在.除此之外,如果我杀死我的应用程序并再次启动它,则再次调用migrate方法,不知道这是否是预期的功能,但是它破坏了我的功能.我认为迁移将仅被调用一次,与onUpgrade()

So after incrementing the version from 1 to 2, the execSQL() is executed but new column is not added in my db. I have pulled my db from app directory and checked multiple times but column is not there. Apart from that if I kill my app and launch it again the migrate method is called again , don't know if this is the intended functionality but it breaks the functionality for me.I thought migrate will be only called once same as onUpgrade()

推荐答案

确保您的列在模型类中.在您的情况下,您要像这样添加列age:ADD COLUMN 'age' INTEGER,因此您的模型类中必须包含int age.

Make sure your column is in model class. In your case, you are adding column age like this: ADD COLUMN 'age' INTEGER, so you must have int age in your model class.

此外,最好是编写迁移测试以确切了解失败的原因.您可以在以下Android文档中找到有关迁移测试的信息: https://developer.android.com/topic/libraries/architecture/room.html#db-migration-testing

Also, it is a good idea to write migration test to known exactly what is failing. You can find about migration test in android documentation here: https://developer.android.com/topic/libraries/architecture/room.html#db-migration-testing

这篇关于房间迁移变更表未添加新列&一次又一次地迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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