Android会议室迁移更改前一列的类型

问题描述 投票:0回答:1

我正在使用房间库来处理我的Android应用程序中的数据库部分。我想从我的db版本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");
    }
};
android database migration
1个回答
0
投票

在你的例外我看到了差异,第一个是:

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'

那么,您是否可以尝试在主键上添加@NonNull注释并尝试再次迁移?

© www.soinside.com 2019 - 2024. All rights reserved.