【发布时间】:2016-12-29 00:30:40
【问题描述】:
我有 2 个领域数据模型。 'Foods' 模型有一个对象属性类型 'foodRating',它旨在作为 FoodsRatings 模型的链接。
class FoodRatings extends Realm.Object {}
FoodRatings.schema = {
name: 'FoodRatings',
primaryKey: 'foodRatingId',
properties: {
foodRatingId: { type: 'int', indexed: true },
name: 'string',
}
};
class Foods extends Realm.Object {}
Foods.schema = {
name: 'Foods',
primaryKey: 'foodId',
properties: {
foodId: { type: 'int', indexed: true },
name: 'string',
foodRating: {type: 'FoodRatings'},
}
};
FoodRatings 预先填充了许多旨在保持静态且不变的行。您可以从 FoodRatings 中的选项之一为食物分配 foodRating。
我遇到的问题是,当我尝试创建一个新的 Foods 对象并将“foodRating”属性分配给 FoodRatings 对象时,它会中断。
foodRatings = realm.objects('FoodRatings')
let foodRating = foodRatings.filtered('foodRatingId = 1');
realm.write(() => {
realm.create('Foods', {foodId: 1, name: 'apples', foodRating: foodRating[0]});
})
我看到很多关于 Realm 的“链接”的讨论,但它尚未针对 Realm js 发布。所有示例和文档都假定新条目而不是您尝试分配给模型的现有对象。
知道我该怎么做吗?
【问题讨论】:
标签: react-native realm