简体   繁体   English

Android 新列的房间自动迁移不起作用

[英]Android Room Auto Migration for new column not working

I have searched stackoverflow for a similar question and none has been found, other possible solutions for different cases did not work for me either.我在 stackoverflow 上搜索了一个类似的问题,但没有找到,针对不同情况的其他可能解决方案对我也不起作用。 I am opening this question seeing that I cannot find a solution for this problem, nor can I find a similar question for this problem.我打开这个问题是因为我找不到解决这个问题的方法,也找不到类似的问题。


I've tried to use Android Room's auto migration feature, but it never works correctly.我试过使用 Android Room 的自动迁移功能,但它永远无法正常工作。

The current scenario is as follows:当前场景如下:

I have a Database version 1我有一个数据库版本 1

I want to add a new column to one table, that is the only modification I want to do.我想在一个表中添加一个新列,这是我想要做的唯一修改。 This column will be holding String values and nothing else.此列将保存 String 值,仅此而已。

This new column will only exist in Database version 2这个新列将只存在于数据库版本 2 中

I have set up the Database as per the Android documentation我已经根据 Android 文档设置了数据库

When I run the app again (an app with a database version 1 populated with data) the app crashes当我再次运行该应用程序时(一个数据库版本 1 填充了数据的应用程序),该应用程序崩溃了

The crash error in the console is this:控制台中的崩溃错误是这样的:

2021-08-24 21:35:53.941 3081-3141/com.app.test E/AndroidRuntime: FATAL EXCEPTION: pool-18-thread-2
    Process: com.app.test, PID: 3081
    java.lang.IllegalStateException: Migration didn't properly handle: My(com.com.app.test.data.datbase.entity.MyEntity).
     Expected:
    TableInfo{name='My', columns=long list of column definitions}
     Found:
2021-08-24 21:35:53.942 3081-3141/com.app.test E/AndroidRuntime: TableInfo{name='My', columns=long list of column definitions, plus the new one}

I have setup the database with the new version number (2), and set export schema to true, and defined the automigrations from 1 to 2.我已经使用新版本号 (2) 设置了数据库,并将导出架构设置为 true,并定义了从 1 到 2 的自动迁移。

I also defined the schemaLocation in the app gradle configuration options.我还在应用程序 gradle 配置选项中定义了 schemaLocation。

I cannot find anywhere explained why this is not working, it is a simple operation of adding a new column to an existing table.我找不到任何地方解释为什么这不起作用,这是向现有表添加新列的简单操作。

Does anyone know why this isn't working and how can I get it to work without having to write the migration manually?有谁知道为什么这不起作用,我怎样才能让它工作而不必手动编写迁移?

EDIT: Upon closer inspection it seems that there is some issue with the defaultValue definition in the database migration.编辑:仔细检查后,数据库迁移中的 defaultValue 定义似乎存在一些问题。

The expected shows the column with a default value that is not null, this is correct defaultValue=''NOT_NOTIFIED''预期显示默认值不是 null 的列,这是正确defaultValue=''NOT_NOTIFIED''

The found shows it is null, this is incorrect defaultValue='NULL'查到显示是null,这个是不正确defaultValue='NULL'

The column definition for that entity is as follows该实体的列定义如下

@ColumnInfo(name = "notified", defaultValue = "NOT_NOTIFIED")
public String notified = "NOT_NOTIFIED";

But somehow it is generating a definition or migration where the default value is null, instead of what I am setting it to.但不知何故,它正在生成默认值为 null 的定义或迁移,而不是我将其设置为的值。

Removing the defaultValue from the column definition in the entity itself appears to do the trick, but it is a requirement to have a default value for that column.从实体本身的列定义中删除defaultValue似乎可以解决问题,但要求该列具有默认值。 This does not appear to be a proper behavior, but a bug somewhere in the auto migration logic.这似乎不是正确的行为,而是自动迁移逻辑中某处的错误。

Hoping someone might have a clue of what is going on and come up with a proper solution that is not to remove the defaultValue definition.希望有人可能知道正在发生的事情并提出一个不删除defaultValue定义的适当解决方案。

I've had the same issue lately, and for me, the issue was that I forgot about a manual migration I've written a while ago that had the same version numbers.我最近遇到了同样的问题,对我来说,问题是我忘记了我前一段时间写的具有相同版本号的手动迁移。

This might not be your case, but for me to figure out why I was getting the wrong migration, I set a breakpoint at the onUpgrade method at RoomOpenHelper.java which led me to the forgotten manual migration.这可能不是你的情况,但为了弄清楚为什么我得到错误的迁移,我在RoomOpenHelper.javaonUpgrade方法中设置了一个断点,这导致我忘记了手动迁移。 so give it a try it might provide you with more information on what's going on.所以试一试它可能会为您提供有关正在发生的事情的更多信息。

Specifying the columnInfo name and default value did the trick for me指定 columnInfo 名称和默认值对我有用

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

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