如何在ROOM迁移期间处理索引信息 [英] how to deal with indices info during ROOM migration

查看:92
本文介绍了如何在ROOM迁移期间处理索引信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

@Entity(tableName = "UserRepo", indices = @Index(value = "id", unique = true))
public class GitHubRepo {
@PrimaryKey(autoGenerate = true)
public int _id;
public int id;
public String name;
public String description;
@Embedded
public RepoOwner owner;

public GitHubRepo(int id, String name, String description, RepoOwner owner) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.owner = owner;
}

public class RepoOwner {
@ColumnInfo(name = "user_id")
public int id;
public String login;

public RepoOwner(int id, String login) {
    this.id = id;
    this.login = login;
}

说明: 我有一个ROOM数据库,其中包含一个简单的表UserRepo,该表包含三列_id, id, name,并使用"id"作为索引,以便进行更快的查询.现在,我想使用@Embedded annotation在此表中添加所有者信息.迁移代码如下:

Explain: I hava a ROOM database which contains a simple table UserRepo, this table contains three columns _id, id, name and use the "id" as the index in order to have a faster query. For now, I want to use the @Embedded annotation to add the owner info in this table. The Migration code is follow:

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE UserRepo ADD COLUMN user_id INTEGER");
        database.execSQL("ALTER TABLE UserRepo ADD COLUMN login TEXT");
    }
};

在迁移期间,我遇到了以下错误:

During the migration, I got an error of:

预期: 表格信息

Expected: TableInfo

{name='UserRepo', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', notNull=true, primaryKeyPosition=1}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=0}, login=Column{name='login', type='TEXT', notNull=false, primaryKeyPosition=0}, user_id=Column{name='user_id', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], **indices=[Index{name='index_UserRepo_id', unique=true, columns=[id]}]**}

发现: TableInfo

Found: TableInfo

{name='UserRepo', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', notNull=true, primaryKeyPosition=1}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=0}, login=Column{name='login', type='TEXT', notNull=false, primaryKeyPosition=0}, user_id=Column{name='user_id', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], **indices=[]**}

似乎我在迁移工具中丢失了索引信息,该如何解决?

Seems I lost the indices info in my Migration implement, How can I fix this?

其他信息我可以通过从GitHubRepo实体中删除索引信息来解决此问题,但是我不想这样做,因为我不想失去索引的优势.

ANOTHER INFO I can fix this issue by remove the indices info from the GitHubRepo entity, But I don't want to do this, as I don't want to lose the advantage of indices.

推荐答案

最后,我通过在Migrate中为此表设置索引来解决此问题.因此代码是:

Finally, I fixed this issue by set an index for this table in Migrate.So the code is:

 public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE UserRepo ADD COLUMN user_id INTEGER");
        database.execSQL("ALTER TABLE UserRepo ADD COLUMN login TEXT");
        database.execSQL("CREATE UNIQUE INDEX index_UserRepo_id ON UserRepo (id)");
    }
};

SQL的第三行是密钥.

The third SQL line is the key.

这篇关于如何在ROOM迁移期间处理索引信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆