Setting the value of a polymorphic belongsTo relationship


#1

I’m trying to set the value of a polymorphic belongsTo, and I keep getting a TypeError: Cannot read property 'modelName' of undefined from ember.

Here are my models:

// handle.js
import DS from "ember-data";
export default DS.Model.extend({
    username: DS.attr('string'),
    incoming: DS.hasMany('request', { async: true, inverse: 'actor'     }),
    outgoing: DS.hasMany('request', { async: true, inverse: 'requestor' })
});

// community.js
import DS from "ember-data";
import Handle from "./handle";
export default Handle.extend({
    name: DS.attr('string')
});

// request.js
import DS from "ember-data";
export default DS.Model.extend({
    requestor: DS.belongsTo('handle', { async: true, polymorphic: true, inverse: 'outgoing' }),
    actor: DS.belongsTo('handle', { async: true, polymorphic: true, inverse: 'incoming' }),
});

Now, I have an existing Community model saved in the community variable, and I’m trying to create a new Request model like so:

this.store.createRecord('request', {
    actor: community
}).save();

Problem is, this generates an error with the following stack trace:

TypeError: Cannot read property 'modelName' of undefined
at ember$data$lib$system$store$$Service.extend.modelFor (http://localhost:4200/assets/vendor.js:70084:22)
at ember$data$lib$system$store$$Service.extend.recordForId (http://localhost:4200/assets/vendor.js:69478:30)
at ember$data$lib$system$store$$deserializeRecordId (http://localhost:4200/assets/vendor.js:70600:27)
at http://localhost:4200/assets/vendor.js:70581:11
at http://localhost:4200/assets/vendor.js:72491:20
at Map.forEach.cb (http://localhost:4200/assets/vendor.js:24194:11)
at OrderedSet.forEach (http://localhost:4200/assets/vendor.js:23987:11)
at Map.forEach (http://localhost:4200/assets/vendor.js:24198:18)
at Function.ember$data$lib$system$model$$default.reopenClass.eachRelationship (http://localhost:4200/assets/vendor.js:72490:83)
at ember$data$lib$system$store$$normalizeRelationships (http://localhost:4200/assets/vendor.js:70577:12)

Is this a bug in Ember or am I doing something wrong?

PS I simplified the models quite a bit, omitting (what I thought was) more unimportant details.


#2

After doing some investigating, it could be a problem with FixtureAdapter. Maybe I need to do some special configuration or something to support polymorphic relationships?

When the TypeError is thrown, the promise returned by save() fails.