简体   繁体   English

如果在Room数据库中添加了新表,是否需要添加迁移?

[英]Is it neccessary to add migration if new table is added in Room database?

I have two tables,person is already existing(added column userid as foreign key) 我有两个表,个人已经存在(添加了列userid作为外键)

    @Entity(tableName = TABLE_NAME)
    data class Person(
    @PrimaryKey @ColumnInfo(name = CLIENT_ID) var id: Long,
    @ColumnInfo(name = USER_ID)var userId : Int = 1) : Parcelable {}

this is the newly added class,which has foreign key of person class(userid) 这是新添加的类,它具有人员类(用户标识)的外键

    @Entity(tableName = Profile.TABLE_NAME,
    foreignKeys = [ForeignKey(entity= Person::class,
    parentColumns = [(Person.USER_ID)],
    childColumns = [(Profile.USER_ID)],
    onDelete = ForeignKey.CASCADE)])
    @Parcelize
    data class Profile(@PrimaryKey(autoGenerate = true)
    var userId: Int,
    var name: String? = null):Parcelable{}

Here i have two tables in the DB. 在这里,我在数据库中有两个表。 Now,do i need to write migration code for creation of new table? 现在,我需要编写迁移代码来创建新表吗?

If you want to migrate your data from the previous version to the new version then 如果要将数据从以前的版本迁移到新版本,则

Yes. 是。 You have to write the migration code. 您必须编写迁移代码。

If you only want to migrate schema and not data. 如果只想迁移架构而不是数据。 then no need to write migration logic. 则无需编写迁移逻辑。 Just add fallbackToDestructiveMigration() in Room builder. 只需在“房间”构建器中添加fallbackToDestructiveMigration()

Example: 例:

Room.databaseBuilder(appContext, AppDatabase.class, AppDatabase.DATABASE_NAME)
            .fallbackToDestructiveMigration()
            .build();

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

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