android - 插入新行时,Android Room FOREIGN KEY 约束失败(代码 787)
问题描述
我关注了这篇文章Android Room FOREIGN KEY constraint failed (code 787)。不幸的是,它不适用于我的情况。
尝试插入新注释时,我总是收到错误消息。当然,数据库中存在一个主题。
你们有什么想法吗?
请看下面我的代码。
话题:
@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 {
}
注道:
@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)
}
解决方案
我发现了问题。因为我已经创建了 2 个不同的数据库:TopicDataBase 和 NoteDataBase。
所以我只需要删除其中的 1 个。我的坏>"<
推荐阅读
- javascript - Allow spaces only at the start and end of the string in JavaScript
- neo4j - 多个 UNWIND 处理空列表
- python - 在 Flask 中渲染(许多)模板
- python - pandas 将 dict 嵌套不平衡 dict 转换为数据框
- apache-flink - Apache Flink 1.4.2 akka.actor.ActorNotFound
- docker - cp-kafka-connect 是开源组件还是专有组件?
- excel - 循环创建数据透视表和条形图
- amazon-ecs - AWS 在更新时更新任务映像标签
- javascript - 您如何将注册表单数据发布到服务器 URL。以及将用户重定向到不同的站点,例如感谢页面。使用 javascript 或 ajax
- r - 如何按ID合并表格,然后合并具有相同ID的单元格