Finding record by custom primaryKey


#1

Hello. My model is defined as follow:

App.User = DS.Model.extend({
  primaryKey: 'username',
  username: DS.attr('string'),
  name: DS.attr('string')
});

My custum Adapter map:

DS.SocketAdapter.map('App.User', {
  primaryKey: 'username',
  username: DS.attr('string'),
});

I am testing this model out by typing on console:

App.User.createRecord({username:"user_1"});
var r = MyTalk.User.find("user_1");
console.log( r.serialize() );

>> Object {username: null, name: null ..... all null}

Also tested:

App.User.find({username:"user_1"});

But this is doing a remote request. I read that Ember Data does allow you to find records via attributes other than the ID.

So what I am doing wrong in telling Ember data my custom primaryKey?

Thanks in advance!


#2

I had a similar issue. Despite what the documentation and everyone else said, this was the only thing that worked for me. I’m on Ember Data 0.13.

App.SocketAdapter.configure('App.User', {
  primaryKey: 'username'
});

Note: It’s the configure method, not map.

I don’t completely understand what’s going on under the hood and the difference between the 3: defining primaryKey in your model, defining it with map, and defining it with configure. Can anyone explain what the 3 things do specifically?


#3

How about with ember-data 1.0.0-beta.6?


#4

+1 for how about doing this configure thing with ember-data-1.0.0-beta.6 .configure apparently doesn’t exist anymore and raises:

Uncaught TypeError: Object function () { if (!wasApplied) { Class.proto(); // prepare prototype... } o_defineProperty(this, GUID_KEY, undefinedDescriptor); o_defineProperty(this, '_super', undefinedDescriptor); var m = met...<omitted>...e' (anonymous function)


#5

wycats explained it pretty concisely here: https://github.com/emberjs/data/issues/1380