房间组合主键链接到外键 [英] Room composite Primary Key link to Foreign Key

查看:198
本文介绍了房间组合主键链接到外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Room数据库实现一个android应用程序,并且对该数据库中的关系有一个小问题.

I am implementing an android application with Room database and have a small question about relationships in this DB.

我有两个表:

@Entity(tableName = "foods", primaryKeys = {"id", "language_id"},
        indices = {@Index(value = {"id", "language_id"}, unique = true)},
        inheritSuperIndices = true)
public class Food {

    @NonNull
    @ColumnInfo(name = "id")
    private String mId;

    @NonNull
    @ColumnInfo(name = "language_id")
    private String mLanguageId;

}

@Entity(tableName = "ingredients", primaryKeys = {"id", "language_id"},
        indices = {@Index(value = {"id", "language_id"}, unique = true), 
        @Index(value = {"food_id", "food_language_id"}, unique = true)},
        foreignKeys = {@ForeignKey(entity = Food.class, parentColumns ="id", 
        childColumns = "food_id", onUpdate = CASCADE, onDelete = CASCADE),
        @ForeignKey(entity = Food.class, parentColumns = "language_id", 
        childColumns = "food_language_id", onUpdate = CASCADE, onDelete = 
        CASCADE)},
        inheritSuperIndices = true)
public class Ingredient {

    @NonNull
    @ColumnInfo(name = "id")
    private String mId;

    @NonNull
    @ColumnInfo(name = "language_id")
    private String mLanguageId;

    @ColumnInfo(name = "food_id")
    private String mFoodId;

    @ColumnInfo(name = "food_language_id")
    private String mFoodLanguageId;

}

两个表"Food"和"Ingredient"都具有复合主键("id","language_id"). Food对象必须包含一个列表,当然还必须包含一个@Relationship

The both tables 'Food' and 'Ingredient' have composite primary keys ('id', 'language_id'). The Food object has to contains a List and of course a @Relationship

public class FoodWithIngredients extends Food{

    @Relation(parentColumn = "id", entityColumn = "food_id", entity = 
    Ingredient.class)
    private List<Ingredient> mIngredients;

}

在我尝试运行此代码后,收到了这些消息

After I try to run this code received these messages

警告:

food_language_id列引用了一个外键,但它不是其中的一部分 索引.每当父表处于活动状态时,这可能会触发全表扫描 已修改,因此强烈建议您创建一个涵盖此内容的索引 列.

food_language_id column references a foreign key but it is not part of an index. This may trigger full table scans whenever parent table is modified so you are highly advised to create an index that covers this column.

错误:

Ingredient has a foreign key (food_id) that references Food (id) but Food does not have a unique index on those columns nor the columns are its primary key. SQLite requires having a unique constraint on referenced parent columns so you must add a unique index to Food that has (id) column(s).

有人可以帮我吗?

先谢谢您了:)

推荐答案

好,我发现我的错误在哪里.我的@ForeignKey是错误的,正确的是这样的:

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)

区别在于我在'parentColumns'和'childColumns'内放置了多列,并且工作正常.

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

@Danail Alexiev插入内容如下:

@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());
        }
    }
}

这里最重要的是,您必须首先插入@Relation的所有者(在此示例中为Food),然后再插入关系中的每个数据

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

这篇关于房间组合主键链接到外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆