簡體   English   中英

房間遷移失敗 IllegalStateException

[英]Room migration is failing IllegalStateException

我現在正在嘗試遷移到房間中的新版本。 我更改了 1 個變量並刪除了一行。

遷移 function

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("CREATE TABLE timereport_table_new (" +
                "id INTEGER, " +
                "company_name TEXT, " +
                "salary REAL, " +
                "hour_salary REAL, " +
                "hours REAL, " +
                "ob_hours REAL, " +
                "ob_hour_salary TEXT," +
                "unpaid_brake REAL," +
                "have_worked INTEGER," +
                "worked_date TEXT )");

        database.execSQL("INSERT INTO timereport_table_new (" +
                "id, " +
                "company_name, " +
                "salary, " +
                "hour_salary," +
                "hours," +
                "ob_hours," +
                "ob_hour_salary," +
                "unpaid_brake," +
                "have_worked," +
                "worked_date) SELECT id, company_name, salary, hour_salary, hours, ob_hours, ob_salary, unpaid_brake, have_worked, worked_Date FROM timereport_table");
        database.execSQL("DROP TABLE timereport_table");
        database.execSQL("ALTER TABLE timereport_table_new RENAME TO timereport_table");
    }
};

model class 的一部分(僅顯示變量)嘗試更改雙倍obSalary; 到字符串 obSalary; 並刪除 //double obSalaryPercent; 從表。

......
@PrimaryKey(autoGenerate = true)
@NonNull int id;
@ColumnInfo(name = "company_name")
String companyName;
double salary;
@ColumnInfo(name = "hour_salary")
double hourSalary;
double hours;
@ColumnInfo(name = "ob_hours")
double obHours;
@ColumnInfo(name = "ob_hour_salary")
double obSalary;
//@ColumnInfo(name = "ob_percent")
//double obSalaryPercent;
@ColumnInfo(name = "unpaid_brake")
double unpaidBrake;
@ColumnInfo(name = "have_worked")
Boolean haveWorked;
@ColumnInfo(name = "worked_date")
String workedDate;
......

收到以下錯誤

E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.example.worktime, PID: 7721
java.lang.RuntimeException: Exception while computing database live data.
    at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)
 Caused by: java.lang.IllegalStateException: Migration didn't properly handle: timereport_table(com.example.worktime.models.TimeReportModel).
 Expected:
TableInfo{name='timereport_table', columns={hour_salary=Column{name='hour_salary', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, hours=Column{name='hours', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, company_name=Column{name='company_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, unpaid_brake=Column{name='unpaid_brake', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, have_worked=Column{name='have_worked', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, ob_hour_salary=Column{name='ob_hour_salary', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, salary=Column{name='salary', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, ob_hours=Column{name='ob_hours', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, worked_date=Column{name='worked_date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='timereport_table', columns={hour_salary=Column{name='hour_salary', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, hours=Column{name='hours', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, company_name=Column{name='company_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, unpaid_brake=Column{name='unpaid_brake', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, have_worked=Column{name='have_worked', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, ob_hour_salary=Column{name='ob_hour_salary', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, salary=Column{name='salary', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, ob_hours=Column{name='ob_hours', type='REAL', affinity='4', notNull=false, primaryKeyPosition=0, defaultValue='null'}, worked_date=Column{name='worked_date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
    at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103)

新表的每個字段的可為空狀態必須與實體中聲明的狀態相匹配。 Java 原始類型隱式不可為空。

此外, obSalary的類型不一致。 在您的實體中聲明為double精度,在您的新表中聲明為TEXT

    database.execSQL("CREATE TABLE timereport_table_new (" +
            "id INTEGER NOT NULL, " +
            "company_name TEXT, " +
            "salary REAL NOT NULL, " +
            "hour_salary REAL NOT NULL, " +
            "hours  REAL NOT NULL, " +
            "ob_hours REAL NOT NULL, " +
            "ob_hour_salary REAL NOT NULL," + // <- changed to REAL
            "unpaid_brake REAL NOT NULL," +
            "have_worked INTEGER," +
            "worked_date TEXT )");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM