Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

android - Room Persistence Library: Weird Error during migration

I am scratching my head with this error. I couldn't find any answer so far. I have old database which I am migrating to Persistence Room library. However whenever I do migration, I am getting following error,

java.lang.IllegalStateException: Migration didn't properly handle. 

Code I am using is as follows:

@Entity(tableName = ROUTE_TABLE)
public class RouteData {

    static final String ROUTE_TABLE = "name";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int id;
    @ColumnInfo(name = "col1")
    private String col1;
    //Other columns with exactly same as 'col1' just different names
    //Getters and Setters
}

For migration,

Room.databaseBuilder(context.getApplicationContext(), MyData.class, DATABASE_NAME)
            .addMigrations(MIGRATION_LATEST)
            .fallbackToDestructiveMigration() 
            .build();

private static final Migration MIGRATION_LATEST = new Migration(9, 10) {
    @Override
    public void migrate(SupportSQLiteDatabase db) {
        //Log.i("Tag" , "Migration Started");
        //Migration logic
        //Log.i("Tag" , "Migration Ended");
    }
};

When I run program. I get "Migration Ended" log in my LogCat but when I am trying access database again, it is giving me following error.

 Caused by: java.lang.IllegalStateException: Migration didn't properly handle xxx.
                                                                              Expected:
                                                                             TableInfo{name='name', columns={col1=Column{name='col1', type='TEXT', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}
                                                                              Found:
                                                                             TableInfo{name='name', columns={col1=Column{name='col1', type='INTEGER', notNull=false, primaryKeyPosition=0}, ....}, foreignKeys=[]}

I have no idea where where this "INTEGER" is coming. I tried uninstalling, invalidating cache and any other closely related solution. Any idea what is going on? It works perfectly fine if you freshly install app. Error is coming up only after migration. According to my log cat, all migration steps seems to be completed but it is still showing migration not handled properly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you all for trying to help me, however I have finally found the answer after days of frustrating debugging. In my app manifest auto backup was on,

android:allowBackup="true"

So while trying out various databases, google was actually backing up my any newly created databases and their structure and restoring them automatically when I was reinstalling app. Hence I was getting this wired error. Once I switch of auto backup android:allowBackup="false" and reinstall app, I can test migration properly.

My suggestion for all developers in future encountering such problem, SWITCH OFF auto backup while development. You can switch it on once you have tested your migration.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...