ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – Room Composite主键的外键

2019-07-05 15:23:31  阅读:1075  来源: 互联网

标签:android sqlite foreign-keys primary-key android-room


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

我有两张桌子:

@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对象必须包含List,当然还有@Relationship

public class FoodWithIngredients extends Food{

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

}

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

WARNNING:

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错了,正确的是:

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

区别在于我在’parentColumns’和’childColumns’中放置了多个列,它的工作正确.

@Danail Alexiev插入是这样的:

@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),之后是关系中的每个数据

标签:android,sqlite,foreign-keys,primary-key,android-room
来源: https://codeday.me/bug/20190705/1388641.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有