1

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.

dlw
  • 103
  • 1
  • 8

1 Answers1

0

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

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Great thanks! I didn't know about onUpdate, but I had already been using onDelete. Still learning sql and android room! Many thanks again. – dlw Aug 30 '22 at 11:14