I am building a database using Room and I can't figure out how to insert the new elements that have a relationship (one to many in my case) into the database. No solution has ever talked about the insertion (they only talk about querying the data).
Here is the DAO:
@Dao
abstract class ShoppingListsDao {
@Insert
abstract suspend fun addNewShoppingList(newShoppingList: ShoppingList)
@Insert
abstract suspend fun addNewItem(newItem: Item)
// This is how I thought it would work but it didn't
@Insert
@Transaction
abstract suspend fun addNewShoppingListWithItems(newShoppingListWithItems: ShoppingListWithItems)
}
Here are my entities:
@Entity
class ShoppingList(
@PrimaryKey(autoGenerate = true)
val listID: Int,
val ListName: String
)
@Entity(foreignKeys = [ForeignKey(
entity = ShoppingList::class,
parentColumns = ["listID"],
childColumns = ["parentListID"]
)])
class Item(
@PrimaryKey(autoGenerate = true)
var itemID: Int,
val name: String,
val quantity: Int,
val parentListID: Int
)