Room Persistence Library: Weird Error during migration

后端 未结 2 1578
说谎
说谎 2020-12-21 00:40

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 migr

相关标签:
2条回答
  • 2020-12-21 00:50

    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.

    0 讨论(0)
  • 2020-12-21 00:50

    The main problem in your migration script is bellow:

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

    Here, in your Entity Class you have :

    @ColumnInfo(name = "col1")
        private String col1; 
    

    but in your migration query you may be adding col1 as INTEGER

    From your error :

    Expected :

    columns={col1=Column{name='col1', type='TEXT', notNull=false, primaryKeyPosition=0}
    

    Found :

    columns={col1=Column{name='col1', type='INTEGER', notNull=false, primaryKeyPosition=0}
    

    Solution: change your migration query like :

    ALTER TABLE 'YOUR_TABLE' ADD COLUMN 'col1' TEXT

    It will solve your problem.

    Thanks :)

    0 讨论(0)
提交回复
热议问题