Room composite Primary Key link to Foreign Key

被刻印的时光 ゝ 提交于 2019-12-04 05:51:08

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);

@Transaction
public 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!