Android Room迁移无法正确处理?

时间:2018-11-09 20:09:08

标签: android kotlin database-migration android-room

我正在迁移房间,但出现此错误

java.lang.IllegalStateException: Migration didn't properly handle words(sharif.vocapower.db.entity.Word).
    Expected:
    TableInfo{name='words', columns={
    name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, 
    mnemonic=Column{name='mnemonic', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0},
    meaning=Column{name='meaning', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, 
    isFavorite=Column{name='isFavorite', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

    Found:
        TableInfo{name='words', columns={
        meaning_bangla=Column{name='meaning_bangla', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0},
        meaning=Column{name='meaning', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, 
        name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, 
        mnemonic=Column{name='mnemonic', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, 
        isFavorite=Column{name='isFavorite', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

我试图添加一个新字段meaning_bangla

这是我的实体类

@Entity(tableName = "words")
public class Word implements Serializable{

    @PrimaryKey
    @ColumnInfo(name = "name")
    @NonNull
    private String name;


@ColumnInfo(name = "meaning")
private String meaning;


@ColumnInfo(name = "mnemonic")
private String mnemonic;

@ColumnInfo(name = "meaning_bangla")
private String meaningBangla;

/*  private String meaningHindi;*/


@ColumnInfo(name = "isFavorite")
private int isFavorite;

@NonNull
public String getName() {
    return name;
}

public void setName(@NonNull String name) {
    this.name = name;
}

public String getMeaning() {
    return meaning;
}

public void setMeaning(String meaning) {
    this.meaning = meaning;
}

public int getIsFavorite() {
    return isFavorite;
}

public void setIsFavorite(int isFavorite) {
    this.isFavorite = isFavorite;
}

public String getMeaningBangla() {
    return meaningBangla;
}

public void setMeaningBangla(String meaningBangla) {
    this.meaningBangla = meaningBangla;
}

public void setMnemonic(String mnemonic) {
    this.mnemonic = mnemonic;
}

public String getMnemonic() {
    return mnemonic;
}

/*  public String getMeaningBangla() {
    return meaningBangla;
}

public void setMeaningBangla(String meaningBangla) {
    this.meaningBangla = meaningBangla;
}

public Word(@NonNull String name, String meaning, String mnemonic,  int isFavorite, String meaningBangla) {
    this.name = name;
    this.meaning = meaning;
    this.mnemonic = mnemonic;
    this.meaningBangla = meaningBangla;
    this.isFavorite = isFavorite;
}

}

我的迁移代码如下所示

            database.execSQL("ALTER TABLE words "
                    + " ADD COLUMN meaning_bangla TEXT");

我正在尝试解决此错误,但没有运气:(任何人都可以帮助我解决此错误。

1 个答案:

答案 0 :(得分:0)

尝试将NOT NULL DEFAULT''添加到迁移中。

database.execSQL("ALTER TABLE words ADD COLUMN meaning_bangla TEXT NOT NULL default ''")