如何更改房间数据库的主键?
How to change primary key of Room database?
更改我的主键需要我更新我的数据库版本。有什么方法可以使用 Room 中的 Migration class 将我的主键从一个字段更改为另一个字段?如果不是,我该如何更改我的主键?以下是我的Entity
.
的片段
@Entity(tableName = "weather")
public class Weather {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
private final String id;
@NonNull
@ColumnInfo(name = "city")
private final String city;
我想从 id
切换到 city
。
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.addMigrations(MIGRATION_1_2).build();
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE weather CHANGE id city VARCHAR(22) NOT NULL;");
}
};
更改我的主键需要我更新我的数据库版本。有什么方法可以使用 Room 中的 Migration class 将我的主键从一个字段更改为另一个字段?如果不是,我该如何更改我的主键?以下是我的Entity
.
@Entity(tableName = "weather")
public class Weather {
@PrimaryKey
@NonNull
@ColumnInfo(name = "id")
private final String id;
@NonNull
@ColumnInfo(name = "city")
private final String city;
我想从 id
切换到 city
。
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.addMigrations(MIGRATION_1_2).build();
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE weather CHANGE id city VARCHAR(22) NOT NULL;");
}
};