我正在开发的应用程序需要能够从api下载一些“对象”以使其可以脱机使用,并且我正在使用带有sqlite的Room将其本地保存在设备上。 对于第一个表,我需要创建一个表,它可以毫无问题地工作,并且一切正常。 问题出在我创建第二张表时,由于某种原因我无法弄清该数据库迁移无法在数据库中创建。
我已经尝试过重新创建实体,清理架构,以便android studio重新创建json文件,但到目前为止没有成功。
实际上,带有架构的json文件已正确创建,但是当我尝试运行该应用程序时,出现以下异常之一:
08-06 15:07:18.558 3844-3844/br.com.neoedu.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.neoedu.app, PID: 3844
java.lang.IllegalStateException: Migration didn't properly handle Resposta(br.com.neoedu.objeto.model.Resposta).
Expected:
TableInfo{name='Resposta', columns={correta=Column{name='correta', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, questao=Column{name='questao', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, resposta=Column{name='resposta', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, img_resposta=Column{name='img_resposta', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='Resposta', columns={}, foreignKeys=[], indices=null}
at br.com.neoedu.common.dao.AppDatabase_Impl$1.validateMigration(AppDatabase_Impl.java:109)
at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:87)
这是一个实体,它的房间无法创建表并遵循模式json。
@Entity
public class Resposta implements RequestModel, Serializable {
@Expose
@SerializedName("id")
@PrimaryKey
private int id;
@Expose
@SerializedName("questao")
@Ignore
private Objeto questao;
@ColumnInfo(name="questao")
private int idQuestao;
@Expose
@SerializedName("resposta")
@ColumnInfo(name = "resposta")
private String resposta;
@Expose
@SerializedName("correta")
@ColumnInfo(name = "correta")
private boolean correta;
@Expose
@SerializedName("imgResposta")
@ColumnInfo(name= "img_resposta")
private String imgResposta;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Objeto getQuestao() {
return questao;
}
public void setQuestao(Objeto questao) {
this.questao = questao;
}
public String getResposta() {
return resposta;
}
public void setResposta(String resposta) {
this.resposta = resposta;
}
public boolean isCorreta() {
return correta;
}
public void setCorreta(boolean correta) {
this.correta = correta;
}
public String getImgResposta() {
return imgResposta;
}
public void setImgResposta(String imgResposta) {
this.imgResposta = imgResposta;
}
public int getIdQuestao() {
return idQuestao;
}
public void setIdQuestao(int idQuestao) {
this.idQuestao = idQuestao;
}```
Schema:
```{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "95d9e6cab68a655a3f82759ed5975ac3",
"entities": [
{
"tableName": "Objeto",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `tipo` TEXT, `tipo_questao` INTEGER NOT NULL, `tri` TEXT, `titulo` TEXT, `descricao` TEXT, `conteudo` TEXT, `tipoConteudo` TEXT, `status` INTEGER NOT NULL, `link` TEXT, `fonte` TEXT, `data_criacao` INTEGER, `revisado_em` INTEGER, `exame` TEXT, `sub_exame` TEXT, `periodo_exame` TEXT, `ano` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "tipo",
"columnName": "tipo",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "tipoQuestao",
"columnName": "tipo_questao",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "tri",
"columnName": "tri",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "titulo",
"columnName": "titulo",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "descricao",
"columnName": "descricao",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "conteudo",
"columnName": "conteudo",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "tipoConteudo",
"columnName": "tipoConteudo",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "status",
"columnName": "status",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "link",
"columnName": "link",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "fonte",
"columnName": "fonte",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "dataCriacao",
"columnName": "data_criacao",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "revisadoEm",
"columnName": "revisado_em",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "exame",
"columnName": "exame",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "subExame",
"columnName": "sub_exame",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "periodoExame",
"columnName": "periodo_exame",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "ano",
"columnName": "ano",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Resposta",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `questao` INTEGER NOT NULL, `resposta` TEXT, `correta` INTEGER NOT NULL, `img_resposta` TEXT, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "idQuestao",
"columnName": "questao",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "resposta",
"columnName": "resposta",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "correta",
"columnName": "correta",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "imgResposta",
"columnName": "img_resposta",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"95d9e6cab68a655a3f82759ed5975ac3\")"
]
}
}
数据库类:
@Database(entities = {Objeto.class, Resposta.class}, version = 2)
@TypeConverters({DateConverter.class, QuestaoConverter.class})
public abstract class AppDatabase extends RoomDatabase {
private static final String DB_NAME = "neoedu.db";
private static volatile AppDatabase instance;
//private AppDatabase(){}
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
}
};
public synchronized static AppDatabase getInstance(Context context){
if(null == instance) {
instance = create(context);
}
return instance;
}
private static AppDatabase create(Context context) {
return Room.databaseBuilder(
context,
AppDatabase.class,
DB_NAME
)
.addMigrations(MIGRATION_1_2)
.allowMainThreadQueries()
.build();
}
public abstract ObjetoDAO getObjetoDao();
public abstract RespostaDAO getRespostaDao();
}
我想念什么?