Room composite Primary Key link to Foreign Key Room composite Primary Key link to Foreign Key sqlite sqlite

Room composite Primary Key link to Foreign Key


Ok, I found where was my mistake. My @ForeignKey was wrong, the right one is this:

@ForeignKey(             entity = Food.class,             parentColumns = {"id", "language_id"},             childColumns = {"food_id", "food_language_id"},             onUpdate = CASCADE, onDelete = CASCADE)

The difference is that I put multiple columns inside 'parentColumns' and 'childColumns' and it works correct.

@Danail Alexiev The insertion is something like this:

@Insert(onConflict = OnConflictStrategy.REPLACE)void insertFoods(List<Food> foods);@Insert(onConflict = OnConflictStrategy.REPLACE)void insertIngredients(List<Ingredient> ingredients);@Transactionpublic void insertFoodData(List<Food> foods, RulesOfGolfDatabase database) {    if (foods != null && database != null) {        insertFoods(foods);        for (Food food : foods) {            insertIngredients(food.getIngrediants());        }    }}

The most important thing here is that you have to insert first the owner of the @Relation (In this example is Food) and after that every data which is in the relationship