Room Pre-packaged database has an invalid schema 列顺序错误

Room Pre-packaged database has an invalid schema column order is wrong

我正在尝试使用 Room 在我的 Android 应用程序中实现关系数据库。 我的问题是在我遵循 Android 开发人员 (1) 上关于多对多关系的教程后发生的错误:

java.lang.IllegalStateException: Pre-packaged database has an invalid schema: appointmentUserCrossRef_table(com.example.myproject.model.entities.AppointmentUserCrossRef
Expected:
TableInfo{name='appointmentUserCrossRef_table', columns={userId=Column{name='userId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2, defaultValue='null'}, appointmentId=Column{name='appointmentId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='appointmentUserCrossRef_table', columns={appointmentId=Column{name='appointmentId', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, userId=Column{name='userId', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices[]}

我有一个预填充的数据库:

CREATE TABLE "users_table" (
    "userId"    INTEGER NOT NULL,
    "firstname" TEXT,
    "lastname"  TEXT,
    "email" TEXT,
    "description"   TEXT,
    "dateOfBirth"   INTEGER,
    "gender"    TEXT,
    "phone" INTEGER NOT NULL,
    "profilePicture"    TEXT,
    "password"  TEXT NOT NULL,
    PRIMARY KEY("userId" AUTOINCREMENT)
);

CREATE TABLE "appointment_table" (
    "appointmentId" INTEGER NOT NULL,
    "name"  TEXT,
    "description"   TEXT,
    "address"   TEXT,
    "latitude"  REAL NOT NULL,
    "longitude" REAL NOT NULL,
    "date"  INTEGER,
    "activityImage" TEXT,
    "author"    INTEGER,
    PRIMARY KEY("appointmentId" AUTOINCREMENT)
);

CREATE TABLE "appointmentUserCrossRef" (
    "appointmentId" INTEGER NOT NULL,
    "userId"    INTEGER NOT NULL
);

我的数据库 class 在 Android:

@Database(entities = {User.class, Appointment.class,  AppointmentUserCrossRef.class}, version = 2, exportSchema = false)
@TypeConverters({
        DateToLongConverter.class,
        UserToIntegerConverter.class
})
public abstract class AppDatabase extends RoomDatabase {
    public abstract AppointmentDao appointmentDao();
    public abstract UserDao userDao();

    private static AppDatabase instance = null;
    public static AppDatabase getInstance() {
        return getInstance(null);
    }

    public static AppDatabase getInstance(Context context) {
        if (instance == null) {
            if (context == null) {
                throw new RuntimeException("no ApplicationContext");
            } else {
                instance = Room.databaseBuilder(context, AppDatabase.class, "database.db")
                        .createFromAsset("database.db")
                        .fallbackToDestructiveMigration()
                        .build();
            }
        }
        return instance;
    }

我的实体:

@Entity(tableName = "appointment_table")
public class Appointment {
    @PrimaryKey(autoGenerate = true)
    public int appointmentId;

    public String name;
    public String description;
    public String address;
    public double latitude;
    public double longitude;
    public Date date;
    public Bitmap image;
    public User author;

@Entity(tableName = "users_table")
public class User {
    @PrimaryKey(autoGenerate = true)
    public int userId;

    public String firstname;
    public String lastname;
    public String email;
    public String description;
    public Date dateOfBirth;
    public String gender;
    public int phone;
    public Bitmap profilePicture;
    public String password;

@Entity(primaryKeys = {"appointmentId", "userId"},
        tableName = "appointmentUserCrossRef_table")
public class AppointmentUserCrossRef {
    public int appointmentId;
    public int userId;
}

public class AppointmentWithUsers {
    @Embedded
    public Appointment appointment;
    @Relation(
            parentColumn = "appointmentId",
            entityColumn = "userId",
            associateBy = @Junction(AppointmentUserCrossRef.class)
    )
    public List<User> userList;
}

还有我的道:

@Dao
public interface AppointmentDao {

    @Insert
    void insert(Appointment appointment);

    @Update
    void update(Appointment appointment);

    @Query("SELECT * FROM appointment_table;")
    LiveData<List<Appointment>> getAll();

    @Transaction
    @Query("SELECT * FROM appointment_table WHERE appointmentId = 1")
    LiveData<List<AppointmentWithUsers>>  getMembers();
}

早些时候我发现了这个问题 () 但这并没有真正帮助我。 有人知道我该如何解决这个问题吗?

最初的 CREATE TABLE 是错误的。

需要在CREATE中指定主键(column_a,column_b)TABLE 架构。

create table my_table (
     appointmentId integer not null,
     userId integer not null,
     primary key (appointmentId , userId )
);

那么您的@Entity 注释就可以正常工作了。