簡體   English   中英

Android - 房間遷移沒有正確處理 CREATE 表

[英]Android - Room Migration didn't properly handle CREATE table

我最近將 Room 庫從 2.2.5 更新為 2.3.0-beta01。 這是我添加的遷移。

"CREATE TABLE 'bookmark' ('id' TEXT, 'content_id' TEXT NOT NULL, 'bookmark_position' INTEGER NOT NULL, 'chapter_index' INTEGER NOT NULL,'chapter_bookmark_position' INTEGER NOT NULL, 'bookmarked_at' INTEGER NOT NULL, 'bookmark_note' TEXT, 'content_type' INTEGER NOT NULL, 'title' TEXT NOT NULL, 'img_url' TEXT, 'sync_pending' INTEGER NOT NULL)"

實體

@Parcelize
@Entity(tableName = "bookmark",
primaryKeys = ["content_id", "bookmark_position", "chapter_index", "chapter_bookmark_position"])
data class Bookmark(
  @SerializedName("id")
  val id: String?,
  @SerializedName("content_id")
  @ColumnInfo(name = "content_id")
  var contentID: String,
  @SerializedName("bookmark_position")
  @ColumnInfo(name = "bookmark_position")
  val bookmarkPosition: Long,
  @SerializedName("chapter_index")
  @ColumnInfo(name = "chapter_index")
  val chapterIndex: Int,
  @SerializedName("chapter_bookmark_position")
  @ColumnInfo(name = "chapter_bookmark_position")
  val chapterBookmarkPosition: Long,
  ..

我在這里使用復合 PK。 在運行應用程序時,我收到了這個錯誤。 唯一的區別是 primarayKeyPosition,在已找到的內容中為 0。

預期的

TableInfo{name='bookmark', columns={bookmark_note=Column{name='bookmark_note', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, content_type=Column{name='content_type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, content_id=Column{name='content_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, bookmark_position=Column{name='bookmark_position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2, defaultValue='null'}, img_url=Column{name='img_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, chapter_index=Column{name='chapter_index', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=3, defaultValue='null'}, bookmarked_at=Column{name='bookmarked_at', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sync_pending=Column{name='sync_pending', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, title=Column{name='title', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, chapter_bookmark_position=Column{name='chapter_bookmark_position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=4, defaultValue='null'}}, foreignKeys=[], indices=[]}

成立

TableInfo{name='bookmark', columns={bookmark_note=Column{name='bookmark_note', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, content_type=Column{name='content_type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, content_id=Column{name='content_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, bookmark_position=Column{name='bookmark_position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, img_url=Column{name='img_url', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, chapter_index=Column{name='chapter_index', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, bookmarked_at=Column{name='bookmarked_at', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sync_pending=Column{name='sync_pending', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, title=Column{name='title', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, chapter_bookmark_position=Column{name='chapter_bookmark_position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

我無法弄清楚出了什么問題。

解決

我忘了添加

PRIMARY KEY(content_id, bookmark_position, chapter_index, chapter_bookmark_position)

在我的原始 SQL 查詢中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM