简体   繁体   中英

Room Database Migration fails with Pre-packaged database has an invalid schema

I've got a simple SQLite database:

sqllite 数据库

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;
  }
}

However, I'm getting this error:

错误图片

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.

在此处输入图像描述

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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