具有唯一索引的房间数据库迁移问题

Raj*_*eem 5 sqlite android database-migration android-room

我试图升级我的数据库版本,在最后一个版本中,该表在迁移室创建了一个唯一索引,给了我以下错误

预期的:

TableInfo{name='ALERT_STATUS', columns={resolution_time=Column{name='resolution_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, event_time=Column{name='event_time', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, is_uploaded=Column{name='is_uploaded', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, InvErrorCode=Column{name='InvErrorCode', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_fault=Column{name='is_fault', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, FaultDetails=Column{name='FaultDetails', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, code=Column{name='code', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, component=Column{name='component', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='index_ALERT_STATUS_code_event_time', unique=true, columns=[code, event_time]}]}
Run Code Online (Sandbox Code Playgroud)

成立:

TableInfo{name='ALERT_STATUS', columns={resolution_time=Column{name='resolution_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, event_time=Column{name='event_time', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, is_uploaded=Column{name='is_uploaded', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, InvErrorCode=Column{name='InvErrorCode', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_fault=Column{name='is_fault', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, FaultDetails=Column{name='FaultDetails', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, code=Column{name='code', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, component=Column{name='component', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='code_time', unique=true, columns=[code, event_time]}]}
Run Code Online (Sandbox Code Playgroud)

我尝试删除唯一索引并且迁移工作正常,但使用唯一索引它从未通过

我的数据库实体在下面

    @Entity(tableName = "ALERT_STATUS" , indices = {@Index(value = {"code", "event_time"},
    unique = true)})

  public class AlertStatus implements Serializable{
    @PrimaryKey(autoGenerate = true)
    public Integer id;

    @NonNull
    public String event_time;
    public String resolution_time;

    @NonNull
    public String code;
    public Integer component;
    public int status;
    public int is_fault;
    public String FaultDetails;
    public String InvErrorCode;
    public int is_uploaded;



    }
Run Code Online (Sandbox Code Playgroud)

Ume*_*ani 6

试试这对我有用。

  1. 创建一个新表,即 Holder 表,它将在结构更改之间保存您的数据。

     database.execSQL("CREATE TABLE TABLE_NAME (id INTEGER, column1 varchar(25), column2 varchar(25) , PRIMARY KEY(id))");
    
    Run Code Online (Sandbox Code Playgroud)
  2. 根据需要创建唯一索引

     database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS <index-name> ON TABLE_NAME (column1,column2)");
    
    Run Code Online (Sandbox Code Playgroud)