[英]not primitive types in Room database migration
I am adding new entity FeedItem in my Room database and writing a migration for it. 我在我的Room数据库中添加了新的实体FeedItem并为其编写了一个迁移。
Problem: I have a Date type in my FeedItem class, which is not primitive type. 问题:我的FeedItem类中有一个Date类型,它不是原始类型。 What is the proper way to write migration in this case?
在这种情况下,编写迁移的正确方法是什么?
@Entity(tableName = "FeedItem")
public class FeedItem implements Item, Votable {
private int id;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "feedItemRowIndex")
private int rowIndex;
private int toId;
private int fromId;
private Date date;
...
my migration currently looks like this. 我的迁移目前看起来像这样。
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE FeedItem (feedItemId INTEGER, " +
"feedItemRowIndex INTEGER, " +
"feedVotes INTEGER" +
"feedVote INTEGER" +
"toId INTEGER" +
"fromId INTEGER" +
"date Date" + // i need to change this row
...
"PRIMARY KEY (feedItemRowIndex))"
and here converter for Date type 这里转换器为日期类型
public class DateConverter {
@TypeConverter
public static Date toDate(Long timestamp) {
return timestamp == null ? null : new Date(timestamp);
}
@TypeConverter
public static Long toTimestamp(Date date) {
return date == null ? null : date.getTime();
}
}
thanks! 谢谢!
If room.schemaLocation is set in your build.gradle file, then you can look at the generated schema and copy the exact sql that Room would use to create the table. 如果在build.gradle文件中设置了room.schemaLocation,那么您可以查看生成的模式并复制Room用于创建表的确切sql。 Complete example here
在这里完成示例
This was quite easy. 这很容易。 Room generates primitive type appropriate to converter type.
Room生成适合转换器类型的原始类型。
So in my case type is INTEGER. 所以在我的情况下类型是INTEGER。 Thanks to @EpicPandaForce for comment
感谢@EpicPandaForce发表评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.