具有 UNIQUE KEY 约束的房间迁移不起作用

Room migration with UNIQUE KEY constraint not working

错误: java.lang.IllegalStateException:迁移未正确处理:insta_alerts(org.altruist.BajajExperia.Models.CEInstaViewDetailsDTO).

预期:

TableInfo{name='insta_alerts', columns={isSeen=Column{name='isSeen', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, smsText=Column{name='smsText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, sentDate=Column{name='sentDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, campaign=Column{name='campaign', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, customerId=Column{name='customerId', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, header=Column{name='header', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, priority=Column{name='priority', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, notificationText=Column{name='notificationText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, alertMessage=Column{name='alertMessage', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_insta_alerts_smsText_customerId', unique=true, columns=[smsText, customerId]}]}

找到:

TableInfo{name='insta_alerts', columns={isSeen=Column{name='isSeen', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, smsText=Column{name='smsText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, sentDate=Column{name='sentDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, campaign=Column{name='campaign', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, customerId=Column{name='customerId', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, header=Column{name='header', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, priority=Column{name='priority', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, notificationText=Column{name='notificationText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, alertMessage=Column{name='alertMessage', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

查询:

database.execSQL("CREATE TABLE IF NOT EXISTS insta_alerts(id INTEGER PRIMARY KEY,campaign TEXT,notificationText TEXT,header TEXT,alertMessage TEXT,priority TEXT,sentDate TEXT,smsText TEXT,customerId TEXT,isSeen INTEGER, CONSTRAINT index_insta_alerts_smsText_customerId UNIQUE(smsText, customerId) ON CONFLICT REPLACE)")

型号:

@Entity(
    tableName = "insta_alerts", indices =
    [Index(
        value = ["smsText", "customerId"],
        unique = true
    )]
)
@Parcelize
data class CEInstaViewDetailsDTO(
    @PrimaryKey(/*autoGenerate = true*/)
    @ColumnInfo(name = "id")
    @SerializedName("id")
    var id: Long? = null,
    @ColumnInfo(name = "campaign")
    @SerializedName("campaign")
    var campaign: String? = null,
    @ColumnInfo(name = "notificationText")
    @SerializedName("notificationText")
    var notificationText: String? = null,
    @ColumnInfo(name = "header")
    @SerializedName("header")
    var header: String? = null,
    @ColumnInfo(name = "alertMessage")
    @SerializedName("alertMessage")
    var alertMessage: String? = null,
    @ColumnInfo(name = "priority")
    @SerializedName("priority")
    var priority: String? = null,
    @ColumnInfo(name = "sentDate")
    @SerializedName("sentDate")
    var sentDate: String? = null,
    @ColumnInfo(name = "smsText")
    @SerializedName("smsText")
    var smsText: String? = null,
    @ColumnInfo(name = "customerId")
    @SerializedName("customerId")
    var customerId: String? = null,
    @ColumnInfo(name = "isSeen")
    @SerializedName("isSeen")
    var isSeen: Int? = null
) : Parcelable

您应该像这样将索引创建添加到迁移中

database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_insta_alerts_smsText_customerId ON insta_alerts (smsText,customerId)")