'How to link deletion in room data

I have two data base or two tables ex:one for matches and one for chat jn event section in match note that the all messages is inserted in one table mean that every chat of every match is inserted in the same table My target is when I delete a group of matches I need to automatically delete all chats of those match with



Solution 1:[1]

You could

  • use an AFTER DELETE TRIGGER, or
  • use a FOREIGN KEY definition with the onDelete action as CASCADE, or
  • do the deletion programmatically (perhaps via a single function that invokes both deletions (the primary being the parent, the other being deletion of the children).

Room doesn't support TRIGGERS via annotation so you would have to create the TRIGGER(s) via code, probably using a CallBack with either, or perhaps both the onCreate or onOpen methods/functions overridden (if in onOpen then you should use CREATE TRIGGER IF NOT EXSITS .... so that if the trigger already exists that it doesn't fail but ignores the creation).

You may wish to refer to https://sqlite.org/lang_createtrigger.html for more information regarding Triggers. For Foreign Keys you may wish to refer to https://developer.android.com/reference/androidx/room/ForeignKey

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MikeT