Room Database Migration doesnt properly handle ALTER TABLE migration

后端 未结 7 2041
渐次进展
渐次进展 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:56

    None of the answers are correct in any of the links. After much experiments, found a way for it. The ALTER query needs to be written in the following way to make it work:

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

    However, the Integer DEFAULT value can be anything.

    If you want to add String type column, add in the following manner:

    database.execSQL("ALTER TABLE 'user' ADD COLUMN 'address' TEXT")
    

    This works like a charm.

提交回复
热议问题