Gautam
Gautam

Reputation: 3362

Room Migration Alter Table not adding new column & migrate getting called again and again

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();
}

Migration property :

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 implementation :

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

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()

Upvotes: 13

Views: 26594

Answers (3)

Boken
Boken

Reputation: 5361

I know it's an old question, but I had the same problem and things that might also help are:

  1. A newly added field needs a default value. If you add a new field, it must have a certain value for old elements in the database. This can be a specific value (e.g. 0 for an Int type) or simply null if the field is nullable.

or

  1. Making changes int the model and starting the application will generate a new .json file with the new(!) schema. So you have a new .json file, with new changes but with the old version (schema) number.

    Remember to make changes in the following order:

    • increasing the database schema version (field version in @Database(...))
    • introducing changes to the data model (@Entity)
    • launching the application

    If you have overwritten the old schema file, simply roll back (revert) the changes, upgrade the database version and generate a new schema. You will then have two json files. Old as it should be, and new with a new field (to be added by automigration).

Upvotes: 2

Raveesh G S
Raveesh G S

Reputation: 682

Try Changing version=2 in AppDatabase inside @Database.

Upvotes: 1

Manish Kumar
Manish Kumar

Reputation: 1125

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.

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

Upvotes: 10

Related Questions