我正在使用房间库来处理我的Android应用程序中的数据库部分。我想从数据库版本3迁移到4。迁移时,我在表中添加了两个新列。但是在迁移时,它将date_time列的类型从TEXT更改为DATETIME。我遇到了以下异常。
java.lang.IllegalStateException: Migration didn't properly handle table_transactions(com.example.braintech.demosmspickerapp.database.TransactionModel).
Expected:
TableInfo{name='table_transactions', columns={entry_type=Column{name='entry_type', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, dg=Column{name='dg', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, balance=Column{name='balance', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, date=Column{name='date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, date_time=Column{name='date_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, reading_grid=Column{name='reading_grid', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, note=Column{name='note', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='table_transactions', columns={entry_type=Column{name='entry_type', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, dg=Column{name='dg', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, balance=Column{name='balance', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, date=Column{name='date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, date_time=Column{name='date_time', type='DATETIME', affinity='1', notNull=false, primaryKeyPosition=0}, reading_grid=Column{name='reading_grid', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, note=Column{name='note', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
实体
@Entity(tableName = AppConstants.TABLE_TRANSACTIONS)
public class TransactionModel implements Serializable {
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "date")
public String date;
@ColumnInfo(name = "balance")
public String balance;
@ColumnInfo(name = "reading_grid")
public String reading_grid;
@ColumnInfo(name = "dg")
public String dg;
@ColumnInfo(name = "date_time")
public String date_time;
@ColumnInfo(name = "note")
public String note;
@ColumnInfo(name = "entry_type")
public String entry_type;
@Ignore
Date dateForCompare;
}
迁移代码
static final Migration MIGRATION_3_4 = new Migration(3, 4) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE '" + AppConstants.TABLE_TRANSACTIONS + "' ADD COLUMN 'note' TEXT ");
database.execSQL("ALTER TABLE '" + AppConstants.TABLE_TRANSACTIONS + "' ADD COLUMN 'entry_type' TEXT ");
Log.d("VROM", "Migration");
}
};
答案 0 :(得分:0)
除了您看到的差异之外,第一个是:
@km
从Expected: id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}
Found: id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}
更改为notNull=true
第二个是:
notNull=false
从Expected: date_time=Column{name='date_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}
Found: date_time=Column{name='date_time', type='DATETIME', affinity='1', notNull=false, primaryKeyPosition=0}
更改为type='TEXT', affinity='2'
那么,您可以尝试在主键上添加type='DATETIME', affinity='1'
注释并尝试再次迁移吗?