安卓。在房间中删除一列时的怪异行为(外键)
我有一个带有房间数据库的Android Note PET项目,我将文件和相关页面存储到文件中。一对多关系:
@Entity(tableName = NotepadDatabase.FILES_TABLE)
data class RoomNoteFile(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "file_name") val name: String,
@ColumnInfo(name = "number_of_pages") val pagesAmount: Int,
)
@Entity(
tableName = NotepadDatabase.PAGES_TABLE,
foreignKeys = [ForeignKey(
entity = RoomNoteFile::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("related_file"),
onDelete = ForeignKey.CASCADE
)]
)
data class RoomNotePage(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "page_text") val text: String,
@ColumnInfo(name = "page_number") val pageNum: Int,
@ColumnInfo(name = "related_file", index = true) val fileId: Int,
)
因此,我注意到,如果我删除文件,而不是用它删除文件页面(我希望它可以使用)。房间更改此文件的所有页面的ID(相关ID),并为它们提供数据库中下一个文件的ID,然后将所有页面移动,最后一个文件的页面被删除。我无法想象会导致那是什么。
I have an android note pet project with room database where I store files and related pages to the file. One to many relationship:
@Entity(tableName = NotepadDatabase.FILES_TABLE)
data class RoomNoteFile(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "file_name") val name: String,
@ColumnInfo(name = "number_of_pages") val pagesAmount: Int,
)
@Entity(
tableName = NotepadDatabase.PAGES_TABLE,
foreignKeys = [ForeignKey(
entity = RoomNoteFile::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("related_file"),
onDelete = ForeignKey.CASCADE
)]
)
data class RoomNotePage(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "page_text") val text: String,
@ColumnInfo(name = "page_number") val pageNum: Int,
@ColumnInfo(name = "related_file", index = true) val fileId: Int,
)
So I noticed that if I delete a file, instead of deleting the file's pages with it (as i expect it to work). Room changes the ids (related_file id) of all the pages of this file and gives them the id of the next file in database, then all the pages shift and the last file's pages get deleted. I can't imagine what might cause that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从提供的代码中,没有问题。因此,您的问题在提供的代码之外。 您可能希望编辑您的问题。但是,您当然应该研究其他代码。也许考虑以下代码以获取一些可以帮助您查明问题的想法,
例如,请考虑使用您提供的代码加上的以下测试/演示加上: -
notepaddatabasedao (即DAO用于插入,删除和提取): -
NotepadDatabase 是一个漂亮的库存@Database注释类,但允许在主线程上进行测试(也许在其他线程上运行是一个问题,是问题,要检查/考虑): -
要测试, mainActivity ,这插入了一些数据(文件,然后是相关页面(每个3个文件,带有3个相关页面)),提取数据并将其输出到日志中,还删除1个文件(ID = 1),然后提取更改的数据输出到日志中: -
结果输出为: -
IE,从已删除的文件中删除,并且没有任何页面关系已更改为属于另一个文件。
从数据库方面,通过应用程序检查,可以看出页面是应有的。即仅剩下6行,与File2和3有关3与File3: -
我相信您需要在您提供的代码中寻找其他问题。
From the supplied code, there is not an issue. So your issue is within other than the supplied code. You may wish to edit your question. However, you should certainly investigate the other code. Perhaps consider the following code to get some ideas that could help you pinpoint the issue(s)
As an example consider the following test/demo that uses the code you have supplied plus:-
NotePadDatabaseDao (i.e. Dao's for inserting, deleting and extracting):-
NotePadDatabase a pretty stock @Database annotated class BUT allows the testing to be run on the main thread (perhaps running on other threads may be an issue, something to check/consider):-
To test, MainActivity, this inserts some data (Files and then Related pages (3 files each with 3 related pages)), extracts the data and outputs it to the log, also deleting 1 File (ID=1), then extracts the changed data outputting it to the log :-
The resultant output being :-
i.e. As far as from the File being deleted that has worked and no Page relationships have changed to belong to another File.
From the database aspect, via App Inspection, it can be seen that the pages are as they should be. i.e. only 6 rows remain and 3 are related to File2 and 3 are related to File3:-
i.e. The CASCADE has done what is expected
As such I believe that you need to look for the issue in other that the code you have provided.
我发现了这个问题,只需查看pager重写已删除文件之后的所有数据的值
I found the issue, it's just view pager rewrites the values of all data that was after the deleted file