gpt4 book ai didi

java - 房间数据库迁移 : java. lang.IllegalStateException:迁移没有正确处理

转载 作者:行者123 更新时间:2023-12-04 14:39:24 27 4
gpt4 key购买 nike

我正在尝试使用房间数据库创建一个新表。我遵循了与程序中已有的第一个表相同的原则,但是当运行应用程序时,我收到了这个错误:

Caused by: java.lang.IllegalStateException: Migration didn't properly handle: WaterFountainEntry(com.mpms.relatorioacessibilidadecortec.entities.WaterFountainEntry).
Expected:
TableInfo{name='WaterFountainEntry', columns={waterFountainHeight=Column{name='waterFountainHeight', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, waterFountainID=Column{name='waterFountainID', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, cupHolderHeight=Column{name='cupHolderHeight', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, schoolEntryID=Column{name='schoolEntryID', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, waterFountApproximation=Column{name='waterFountApproximation', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[ForeignKey{referenceTable='SchoolEntry', onDelete='CASCADE', onUpdate='CASCADE', columnNames=[schoolEntryID], referenceColumnNames=[cadID]}], indices=[]}
Found:
TableInfo{name='WaterFountainEntry', columns={waterFountainID=Column{name='waterFountainID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, waterFountainHeight=Column{name='waterFountainHeight', type='DOUBLE', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, cupHolderHeight=Column{name='cupHolderHeight', type='DOUBLE', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, schoolEntryID=Column{name='schoolEntryID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, waterFountApproximation=Column{name='waterFountApproximation', type='DOUBLE', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[ForeignKey{referenceTable='SchoolEntry', onDelete='NO ACTION', onUpdate='NO ACTION', columnNames=[schoolEntryID], referenceColumnNames=[cadID]}], indices=[]}
通过阅读此错误消息,我确实收集到程序期望某些字段的类型不同(并且可能也与它们不同的顺序),但事实是它不应该是那样的。我确保完全按照将字段放入实体中的方式编写迁移类,如下所示: <Migration Class and Database Creation>
 static final Migration MIGRATION_2_3 = new Migration(2,3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE WaterFountainEntry (waterFountainID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
"schoolEntryID INTEGER NOT NULL, waterFountainHeight DOUBLE NOT NULL, cupHolderHeight DOUBLE NOT NULL," +
"waterFountApproximation DOUBLE NOT NULL, FOREIGN KEY (schoolEntryID) REFERENCES SchoolEntry (cadID))");
}
};

public static ReportDatabase getDatabase(final Context context) {
if (INSTANCE == null){
synchronized (ReportDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), ReportDatabase.class, "ReportDatabase")
.addCallback(roomCallback).addMigrations(MIGRATION_2_3).build();
}
}
}
return INSTANCE;
}
<WaterFountainEntry entity>
@Entity(foreignKeys = @ForeignKey(entity = SchoolEntry.class, parentColumns = "cadID", childColumns = "schoolEntryID",
onDelete = CASCADE, onUpdate = CASCADE))
public class WaterFountainEntry {

@PrimaryKey(autoGenerate = true)
@NonNull
private Integer waterFountainID;
@NonNull
private Integer schoolEntryID;
@NonNull
private Double waterFountainHeight;
@NonNull
private Double cupHolderHeight;
@NonNull
private Double waterFountApproximation;

/** getters & setters **/

public WaterFountainEntry(@NonNull Integer schoolEntryID, @NonNull Double waterFountainHeight,
@NonNull Double cupHolderHeight, @NonNull Double waterFountApproximation) {
this.schoolEntryID = schoolEntryID;
this.waterFountainHeight = waterFountainHeight;
this.cupHolderHeight = cupHolderHeight;
this.waterFountApproximation = waterFountApproximation;
}
那么,可能是什么错误?我真的阅读了很多文档,找不到这个问题的原因。

最佳答案

获取正确 SQL 的简单技巧是让 Room 代表您生成 SQL。
创建实体然后编译(Cntrl+F9)然后查看生成的java(从Android View )然后查找与后缀为_Impl的@Database类相同的文件,然后找到创建所有表 方法和SQL在那里。这正是 Room 所期望的。
例如:-
enter image description here

关于java - 房间数据库迁移 : java. lang.IllegalStateException:迁移没有正确处理 <table_name>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67307629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com