简体   繁体   English

带有索引实体的会议室数据库迁移

[英]Room Database Migration with Indexed Entity

I have 'ImagePendingDBStore' entity, I have added 4 more columns in this entity (see comment in code below). 我有'ImagePendingDBStore'实体,在此实体中添加了4列(请参见下面的代码中的注释)。 In this, one column is indexed 'referenceId' 在此,将一列索引为“ referenceId”

@Entity(tableName = "ImagePendingDBStore",indices = {@Index(value = "referenceId")})
public class ImagePendingDBStore {

 @PrimaryKey(autoGenerate = true)
 @NonNull
 private int ImageId;
 private String referenceId;
 private Date createdAt;
 private Date updatedAt;
 private String imageAbsolutePath;
 private int synced = 0;
 private String customerFirstName;
 private String customerMiddleName;
 private String customerLastName;
 private String dasId;
 private String documentName;
 private String imageIdRandom;
 private int imageResolution;

 //new columns
 private String dealerId;
 private String lat;
 private String lng;
 private Date imageCaptureDate;
}

My migration logic in database class is as below: 我在数据库类中的迁移逻辑如下:

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN dealerId TEXT");
        database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN lat TEXT");
        database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN lng TEXT");
        database.execSQL("ALTER TABLE ImagePendingDBStore ADD COLUMN imageCaptureDate DATE");
        database.execSQL("CREATE  INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)");
    }
};

Below is the error I am facing: 以下是我面临的错误:

Caused by: android.database.sqlite.SQLiteException: index index_ImagePendingDBStore_referenceId already exists (code 1): , while compiling: CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (index index_ImagePendingDBStore_referenceId already exists (code 1): , while compiling: CREATE INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId))
    #################################################################

As the message says index_ImagePendingDBStore_referenceId already exists. 如消息所示, index_ImagePendingDBStore_referenceId已经存在。

You only need the 4 ALTER statements as the index exists ie you have not added it. 您仅需要4条ALTER语句,因为该索引存在,即您尚未添加它。 As it already exists then adding the 4 columns doesn't require the index to be changed. 由于已经存在,因此添加4列不需要更改索引。

You could remove the line :- 您可以删除以下行:

database.execSQL("CREATE  INDEX index_ImagePendingDBStore_referenceId ON ImagePendingDBStore (referenceId)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM