简体   繁体   English

房间自动迁移不起作用:新表(实体)的致命异常

[英]Room Auto migration not works : Fatal exception for new Table (Entity)

After trying to use auto migration for a new entity/table i found this error.在尝试对新实体/表使用自动迁移后,我发现了这个错误。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.sharetrip.b2b, PID: 8861
java.lang.IllegalStateException: Migration didn't properly handle: QuickPassenger(net.sharetrip.b2b.view.more.model.QuickPassenger).
 Expected:
TableInfo{name='QuickPassenger', columns={passportNumber=Column{name='passportNumber', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, lastName=Column{name='lastName', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wheelChair=Column{name='wheelChair', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, gender=Column{name='gender', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mealPreference=Column{name='mealPreference', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mobileNumber=Column{name='mobileNumber', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, dateOfBirth=Column{name='dateOfBirth', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, firstName=Column{name='firstName', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, seatPreference=Column{name='seatPreference', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, titleName=Column{name='titleName', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, nationality=Column{name='nationality', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, frequentFlyerNumber=Column{name='frequentFlyerNumber', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, passportCopy=Column{name='passportCopy', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, passportExpireDate=Column{name='passportExpireDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, visaCopy=Column{name='visaCopy', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, travellerType=Column{name='travellerType', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='QuickPassenger', columns={}, foreignKeys=[], indices=[]}
    at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103)

It seems that no columns found when the migration occurs.迁移发生时似乎没有找到任何列。 I declared my entitity like below.我像下面这样声明了我的实体。

@Entity
data class QuickPassenger(
    @PrimaryKey
    @ColumnInfo(name = "id")
    var id: String,
    var titleName: String?,
    var firstName: String?,
    var lastName: String?,
    var gender: String? = Gender.male,
    var nationality: String = "BD",
    var dateOfBirth: String?,
    var passportNumber: String?,
    var frequentFlyerNumber: String?,
    var passportExpireDate: String?,
    var seatPreference: String?,
    var mealPreference: String?,
    var wheelChair: String?,
    var passportCopy: String?,
    var visaCopy: String?,
    val travellerType: String?,
    val email: String?,
    val mobileNumber: String?
)

I am using 2.4.0-alpha04 version of room for all我用的是2.4.0-alpha04版本的room for all

androidx.room:room-compiler 
androidx.room:room-ktx
androidx.room:room-runtime. 

Any help is highly appreciated.非常感谢任何帮助。

It's related to your database version and schemas.它与您的数据库版本和模式有关。 After entity creation (or other database schemas changes), if you build and run the project without increasing the database version or defining the auto-migration, then the app database may be damaged.实体创建(或其他数据库架构更改)后,如果您在不增加数据库版本或定义自动迁移的情况下构建并运行项目,则应用程序数据库可能会损坏。
So, you need to increase the database version (eg from v1 to v2) and define the auto-migration (as the docs ) first, then build and run the project.因此,您需要先增加数据库版本(例如从 v1 到 v2)并定义自动迁移(如docs ),然后构建并运行项目。
In your case (that the app database is probably broken), I suggest you uninstall the app, rollback (or stash) your changes, run the project, apply all the entity creation and auto-migration stuffs again, and re-run the project.在你的情况下(应用程序数据库可能已损坏),我建议你卸载应用程序,回滚(或隐藏)你的更改,运行项目,再次应用所有实体创建和自动迁移的东西,然后重新运行项目.
But before re-run, be sure that your old database schemas file has not changed (look for file 1.json as my example).但在重新运行之前,请确保您的旧数据库模式文件没有更改(查找文件1.json作为我的示例)。 Instead, by increasing the database version and re-run the project, the new database schemas file should be auto-generated (look for file 2.json as my example).相反,通过增加数据库版本并重新运行项目,应该会自动生成新的数据库模式文件(查找文件2.json作为我的示例)。

Note) The official Google docs may be out-of-date (This is the case at the time of writing,), so I can offer this article about Room auto-migrations .注意)官方 Google 文档可能已过时(在撰写本文时就是这种情况),因此我可以提供这篇关于Room auto-migrations 的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM