Android Room FOREIGN KEY constraint failed (code 787)

后端 未结 2 658
轮回少年
轮回少年 2020-12-18 15:39

I\'m tying to create a database in Android Room with two foreign keys. Every time I try to insert a track into the database the program chrashes and says that the \"foreign

相关标签:
2条回答
  • 2020-12-18 16:29

    You need to have rows in tables Kategorie and Playlist with a same foreign key as has Track that you are inserting.

    0 讨论(0)
  • 2020-12-18 16:30

    There's another reason why this error occurs which consumed a lot of my time since every possible solution available on the internet seemed futile in my case.

    If you use rowId of the parent table as the parentColumn key, the Foreign key constraint CANNOT be used.

    As per the SQLite documentation on Foreign Keys:

    The parent key is the column or set of columns in the parent table that the foreign key constraint refers to. This is normally, but not always, the primary key of the parent table. The parent key must be a named column or columns in the parent table, not the rowid.

    0 讨论(0)
提交回复
热议问题