Room Database Migration doesnt properly handle ALTER TABLE migration

后端 未结 7 2012
渐次进展
渐次进展 2020-12-13 06:36

Java.lang.IllegalStateException

Migration didn\'t properly handle user(therealandroid.github.com.roomcore.java.User).

Expected:

TableInfo{n

7条回答
  •  伪装坚强ぢ
    2020-12-13 06:59

    The error message is hard to parse, but there's a difference:

    TableInfo{name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, age=Column{name='age', type='INTEGER', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}}, foreignKeys=[]} Found:

    Found

    TableInfo{ name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, age=Column{name='age', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[]}

    Age is nullable but Room expected it to be not null.

    Change your migration to:

    database.execSQL("ALTER TABLE 'user' ADD COLUMN 'age' INTEGER NOT NULL");
    

    Since this exception explanation is VERY difficult to parse, I have created a small script that does the diff for you.

    Example:

    mig "java.lang.IllegalStateException: Migration failed. expected:TableInfo{name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, age=Column{name='age', type='INTEGER', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}}, foreignKeys=[]} , found:TableInfo{name='user', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, age=Column{name='age', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[]}"
    

    Result:

提交回复
热议问题