Room 数据库迁移失败,预打包数据库的架构无效 [英] Room Database Migration fails with Pre-packaged database has an invalid schema

查看:54
本文介绍了Room 数据库迁移失败,预打包数据库的架构无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 SQLite 数据库:

I've got a simple SQLite database:

对应的用户实体类如下所示:

The corresponding User Entity class looks like follows:

@Entity
public class User {
  @PrimaryKey
  public int uid;

  @ColumnInfo(name = "first_name")
  public String firstName;

  @ColumnInfo(name = "last_name")
  public String lastName;

  public User(int uid, String firstName, String lastName){
      this.uid = uid;
      this.firstName = firstName;
      this.lastName = lastName;
  }
}

但是,我收到此错误:

我错过了什么?列顺序错了吗?我认为这是由于 notNull 注释没有在我的实体类中设置,但即使有注释,错误仍然存​​在.

What am I missing? Is the column sequence wrong? I assumed it is due to the notNull annotation which was not set in my entity class, but even with the annotation the error remains.

推荐答案

您为uid"分配了 NN(非空)值的问题SQLite 中的列,并且出现在错误日志的 Expected 部分,尽管在迁移代码(您没有显示)中,您没有明确添加NOT NULL"对迁移的声明,以及在错误日志的 Found 部分可以看到的内容.

The problem that you assigned a NN (Non-Null) value to the "uid" column in SQLite, and that appears in Expected section of the error log, although In the migration code (that you didn't show), you didn't explicitly add "NOT NULL" statement to the migration, and that what can be seen in the Found section of the error log.

顶部图像以绿色突出显示.

This is highlighted in green the top image.

所以,应该会看到类似下面的内容,根据你的代码检查和修改:

So, it's expected to see something like below, check and modify according to your code:

database.execSQL("ALTER TABLE 'User' ADD COLUMN 'uid' INTEGER NOT NULL");

注意:在文本中添加错误日志比添加图像更有帮助,尽管在您的情况下这将有助于为插图着色:)

NB: Adding error logs in text is more helpful than adding images, although in your case that would help in coloring it for illustration :)

这篇关于Room 数据库迁移失败,预打包数据库的架构无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆