为领域创建主键

马克西米利安·金斯霍夫

我使用 Realm 作为我的 react-native 应用程序的数据库后端。由于 Realm 不提供主键的自动创建,我只会将最后一个已知 ID 增加 1。

我怎样才能获得最后一个已知的 ID?

古库尔

使用这个函数逻辑

class MySchema extends Realm.Object {}
MySchema.schema = {
 name: "MySchema", 
 primaryKey: 'id',
 properties: {
  id: "int",
  myProperty: "string", 
 }
const myRealm = new Realm({
    schema: MySchema,
})
getPrimaryKeyId(model) {
if (myRealm .objects(model).max("id")) {
  return myRealm.objects(model).max("id") + 1;
}
return 1;
}
insertQuery(){
 myRealm..write(() => {
 realm.create("MySchema",{id:getPrimaryKeyId('MySchema'), myProperty:'propertyValue'});
 })
}

}`

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章