Schema does not have a primary key


#1

Hello Forum,

I am newbie to Realm with react native. I am preparing one basic crud operation. I have successfully created INSERT AND VIEWING data with Realm but while deleting data with respect to primaryKey I have created. Its giving me this error
Error: Userschema does not have primary key defined.

Although I have already mentioned primary key in schema.

I update version also just for checking if it did not update primary key for the first time.
He is my code snippet.

//all schema schema
import Realm from ‘realm’;

export const USER_SCHEMA = “Userschema”;

//defining model for schema
export const userSchema = {
name: USER_SCHEMA,
primeryKey: ‘id’,
properties:{
id: ‘string’,
firstname:{type:‘string’, indexed: true},
lastname:{type:‘string’, indexed:true}
}
}

export const updateduserSchema = {
name: USER_SCHEMA,
primeryKey: ‘id’,
properties:{
id: ‘string’,
firstname:{type:‘string’, indexed: true},
lastname:{type:‘string’, indexed:true}
}
}
var defaultPath = Realm.defaultPath;
var newPath = defaultPath.substring(0, defaultPath.lastIndexOf(’/’)) + ‘/userapp.realm’;
const databaseOptions = {
path: newPath,
schema:[updateduserSchema],
schemaVersion:2
}

This is my delete query which is not working

//deleting user here
export const deleteUserList = deleteuserId => new Promise((resolve, reject) => {
Realm.open(databaseOptions).then(realm=> {
realm.write(() => {
alert(‘deleting id===>’+deleteuserId);
alert(typeof deleteuserId);
let deletingUserList = realm.objectForPrimaryKey(USER_SCHEMA, deleteuserId);

        realm.delete(deletingUserList);
        resolve();
    });
});

}).catch((error) => reject(error));

This is my insert function which is working properly.

//functions for userlist
export const insertNewUser = newUser => new Promise((resolve, reject)=>{
Realm.open(databaseOptions).then(realm=>{
realm.write(() => {
realm.create(USER_SCHEMA, newUser);
resolve(newUser);
});
}).catch((error) => reject(error));
});

Thanks in advance.


#2

I think it is a simple typing mistake: You write primeryKey but it should be primaryKey.


#3

@kneth Thanks for you answer. It worked! :slight_smile: