Android 插入新行时 Room FOREIGN KEY 约束失败(代码 787)
Android Room FOREIGN KEY constraint failed (code 787) when insert new row
我关注了这个post。不幸的是,它不适用于我的情况。
我在尝试插入新笔记时总是遇到错误。当然,数据库中存在一个Topic。
你们有什么想法吗?
请看下面我的代码。
主题:
@Entity(tableName = "topic")
data class Topic(@PrimaryKey var id: Long? = null,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "note_count") var noteCount: Int = 0,
@ColumnInfo(name = "view_count") var viewCount: Long = 0) : Serializable {
@Ignore
var notes: List<Note>? = null
}
注:
@Entity(tableName = "note",
foreignKeys = arrayOf(ForeignKey(entity = Topic::class,
parentColumns = arrayOf("id"), childColumns = arrayOf("topic_id"), onDelete = ForeignKey.CASCADE)))
@TypeConverters(TimestampConverter::class)
data class Note(@PrimaryKey(autoGenerate = true) var noteId: Long? = null,
@ColumnInfo(name = "topic_id") var topicId: Long? = null,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "caption") var caption: String? = "",
@ColumnInfo(name = "created_at") var createdAt: Date? = null) : Serializable {
}
NoteDao:
@Dao
interface NoteDao {
@Query("SELECT * from note")
fun getAll(): LiveData<List<Note>>
@Insert(onConflict = REPLACE)
fun insert(note: Note)
@Query("DELETE from note")
fun deleteAll()
@Delete
fun delete(category: Note)
@Query("SELECT * FROM note WHERE title = :title COLLATE NOCASE LIMIT 1")
fun findNoteByTitle(title: String): Note?
@Query("SELECT * FROM note WHERE topic_id = :topicId")
fun findNotesByTopicId(topicId: Long): LiveData<List<Note>>
@Query("SELECT count(*) FROM note WHERE topic_id = :topicId")
fun countNotesByTopicId(topicId: Long): Long
@Update(onConflict = IGNORE)
fun update(category: Note)
}
(@PrimaryKey var id: Long? = null,
看起来像个问题。主键不能为空
我发现了问题。因为我创建了 2 个不同的数据库:TopicDataBase 和 NoteDataBase。
所以我只需要删除其中的 1 个。
我的坏 >"<
我关注了这个post
我在尝试插入新笔记时总是遇到错误。当然,数据库中存在一个Topic。
你们有什么想法吗?
请看下面我的代码。
主题:
@Entity(tableName = "topic")
data class Topic(@PrimaryKey var id: Long? = null,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "note_count") var noteCount: Int = 0,
@ColumnInfo(name = "view_count") var viewCount: Long = 0) : Serializable {
@Ignore
var notes: List<Note>? = null
}
注:
@Entity(tableName = "note",
foreignKeys = arrayOf(ForeignKey(entity = Topic::class,
parentColumns = arrayOf("id"), childColumns = arrayOf("topic_id"), onDelete = ForeignKey.CASCADE)))
@TypeConverters(TimestampConverter::class)
data class Note(@PrimaryKey(autoGenerate = true) var noteId: Long? = null,
@ColumnInfo(name = "topic_id") var topicId: Long? = null,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "caption") var caption: String? = "",
@ColumnInfo(name = "created_at") var createdAt: Date? = null) : Serializable {
}
NoteDao:
@Dao
interface NoteDao {
@Query("SELECT * from note")
fun getAll(): LiveData<List<Note>>
@Insert(onConflict = REPLACE)
fun insert(note: Note)
@Query("DELETE from note")
fun deleteAll()
@Delete
fun delete(category: Note)
@Query("SELECT * FROM note WHERE title = :title COLLATE NOCASE LIMIT 1")
fun findNoteByTitle(title: String): Note?
@Query("SELECT * FROM note WHERE topic_id = :topicId")
fun findNotesByTopicId(topicId: Long): LiveData<List<Note>>
@Query("SELECT count(*) FROM note WHERE topic_id = :topicId")
fun countNotesByTopicId(topicId: Long): Long
@Update(onConflict = IGNORE)
fun update(category: Note)
}
(@PrimaryKey var id: Long? = null,
看起来像个问题。主键不能为空
我发现了问题。因为我创建了 2 个不同的数据库:TopicDataBase 和 NoteDataBase。
所以我只需要删除其中的 1 个。 我的坏 >"<