title: Android Room Persistence Library: Upsert
body: <p>Android's Room persistence library graciously includes the @Insert and @Update annotations that work for objects or collections. I however have a use case (push notifications containing a model) that would require an UPSERT as the data may or may not exist in the database.</p>
  
  <p>Sqlite doesn't have upsert natively, and workarounds are described in this <a href="https://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace">SO question</a>. Given the solutions there, how would one apply them to Room?</p>
  
  <p>To be more specific, how can I implement an insert or update in Room that would not break any foreign key constraints? Using insert with onConflict=REPLACE will cause the onDelete for any foreign key to that row to be called. In my case onDelete causes a cascade, and reinserting a row will cause rows in other tables with the foreign key to be deleted. This is NOT the intended behavior.</p>