In my android application, I have a parent and child database in a one-many relation, using a foreignkey to bind a column of the child database with an element of the parent.
The parent has columns
period [acts as ID] | integer |
---|---|
classical | 1 |
romantic | 2 |
and the child has
parentName | firstname | lastname |
---|---|---|
romantic | Gustav | Mahler |
romantic | Richard | Wagner |
classical | Wolfgang Amadeus | Mozart |
I want the user to be able to update the parent name. When the child is empty I can do this by having a method in the parent dao:
@Query("UPDATE ParentTable SET period = :newPeriod WHERE period = :oldPeriod")
void updateListName(String oldPeriod,String newPeriod);
But as soon as the child database becomes nonempty, this doesn't seem to work! How do I do this! Clearly, I also need all the child entities to be able to update.
Many thanks.
CodePudding user response:
Within the @ForeignKey
annotation, you can use onUpdate = ForeignKey.CASCADE
,
- you may also want to use
onDelete = ForeignKey.CASCADE
For more details you can refer to https://www.sqlite.org/foreignkeys.html#fk_actions