[英]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。也就是说
Int
和String
不可为空。 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也就是说,如果你
@Entity
annotated class(es) as required@Entity
注释类@Entity
annotated class(es) to the entities
parameter of the @Database
annotated abstract class.@Entity
注释类添加到@Database
注释摘要 class 的entities
参数中。@Database
annotated class, but suffixed with _Impl .@Database
,注释为 class,但后缀为_Impl 。createAllTables
method, and you will find code that exceutes the SQL for creating the tables with EXACTLY the SQL that Room expects.createAllTables
方法,您将找到执行 SQL 的代码,用于创建完全符合 Room 预期的 SQL 的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.