简体   繁体   English

如何在 Room 数据库中使用迁移创建表?

[英]How to creatle table using migration in Room database?

 Expected:
    TableInfo{name='role', columns={title=Column{name='title', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}
     Found:
    TableInfo{name='role', columns={id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, title=Column{name='title', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

I'm trying to make migration and I don't understand where is the problem.我正在尝试进行迁移,但我不明白问题出在哪里。 I see title is the first row, but I do not understand why.我看到标题是第一行,但我不明白为什么。 In RoleRoom we can see directly it is on the second place... How to fix it?RoleRoom我们可以直接看到它在第二位...如何解决? :) :)

@TypeConverters(RoomConverters::class)
@Entity(tableName = "role")
class RoleRoom(
    @PrimaryKey val id: Int = 0,
    val title: String
)

in Migration class在迁移 class

        database.execSQL("CREATE TABLE IF NOT EXISTS `role` (`id` INTEGER, `title` TEXT, PRIMARY KEY(`id`))")

The issues are that Room expects non nullables to be NOT NULL. That is Int and String are not nullable.问题是 Room 期望不可为空的值不是 NULL。也就是说IntString不可为空。 So Room expects所以房间期望

CREATE TABLE IF NOT EXISTS `role` (`id` INTEGER NOT NULL, `title` TEXT NOT NULL, PRIMARY KEY(`id`))")

Hence the expected has .... notNull=true.... notNull=true whilst the found has .... notNull=false.... notNull=false因此预期有.... notNull=true.... notNull=true而发现有.... notNull=false.... notNull=false

However, rather than trying to interpret the @Entity annotated class, it is simpler to let Room do the work.但是,与其尝试解释带注释的@Entity class,不如让 Room 完成工作更简单。 That is if you也就是说,如果你

  1. Create the @Entity annotated class(es) as required根据需要创建@Entity注释类
  2. Add the @Entity annotated class(es) to the entities parameter of the @Database annotated abstract class.@Entity注释类添加到@Database注释摘要 class 的entities参数中。
  3. Compile the project (CTRL + F9).编译项目 (CTRL + F9)。
  4. From the Android View look for the java(generated) folder/directory, expand the directories and locate the class that is the same name as the @Database annotated class, but suffixed with _Impl .从 Android 视图中查找 java(生成的)文件夹/目录,展开目录并找到与 @Database 同名的@Database ,注释为 class,但后缀为_Impl
  5. Within the class, locate the createAllTables method, and you will find code that exceutes the SQL for creating the tables with EXACTLY the SQL that Room expects.在 class 中,找到createAllTables方法,您将找到执行 SQL 的代码,用于创建完全符合 Room 预期的 SQL 的表。
    1. Note that the code for room_master_table should be ignored as Room will create the table and populate it itself.请注意, room_master_table的代码应该被忽略,因为 Room 将创建表并自行填充它。
  6. Copy the respective SQL and use that.复制相应的 SQL 并使用它。

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

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