I have modeled a many-to-many relationship with Room based on the docs and this medium article. Using this @Relation I can retrieve RecipeWithIngredients or IngredientWithRecipe Objects from the database. For this to work I need to insert a recipe to the recipe table, an ingredient to the ingredient table then a reference with both of their ids to the reference table first. I don't think there is another way of doing insertion other than using @Transaction on the Dao and just doing it all on one method, however for deletion and updating shouldn't I be able to do those using the Relation as well? The documentation on Room looks a bit lacking to me and so I've come to you
//Get for recipeWithIngredients object. (This works)
@Transaction
@Query("SELECT * FROM Recipe WHERE recipeID = :ID")
suspend fun getRecipeWithIngredients(ID:Long): RecipeWithIngredients
//Insert recipeWithIngredients object. (I don't see why this wouldn't work)
//IDs are autoGenerated so I can't get them until they are inserted
@Transaction
suspend fun insertRecipeWithIngredients(recipeWithIngredients: RecipeWithIngredients){
val recipeID = insertRecipe(recipeWithIngredients.recipe)
for(ingredient in recipeWithIngredients.ingredients){
val ingredientID = insertIngredient(ingredient)
insertRecipeIngredientRef(recipeID, ingredientID )
}
}
//I could try doing this like the insert but should't there be a way to do it like the get?
@Transaction
@Query("SOME SQL HERE")
suspend fun deleteRecipeWithIngredients(recipeWithIngredients: RecipeWithIngredients)
