Room 复合主键 link 到外键
Room composite Primary Key link to Foreign Key
我正在使用 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;
}
在我尝试 运行 此代码收到这些消息后
警告:
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),然后是关系中的每个数据
我正在使用 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;
}
在我尝试 运行 此代码收到这些消息后
警告:
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),然后是关系中的每个数据