Creating Models for Many to Many Relationship


I have two models. Lets call them mult and post. They have a many to many relationship.

Their definitions looks like:


export default Model.extend({
  name: attr(),
  mult: hasMany('mult')


export default Model.extend({
  name: attr(),
  perspectives: hasMany('post')

I want to start off by creating a Post and adding Mults to it. So, I do

createPost(name) {
  let post = this.get('store').createRecord('post', {
    name: name, mults: []

in my controller.js. However, this then throws an exception of

mult.js:3 Uncaught TypeError: (0 , _emberData.default) is not a function

and it is referring to this line of mult:

name: (0, _emberData['default'])(),

Can I create a post first without providing any mults? If so, what might be a suggested workaround?

Note: I am using Ember 2.6.


I’m guessing you are using an identifier out of scope, and that’s compiling weird. Did you import the names of hasMany and attr, and such? Perhaps you are importing them like

import attr from 'ember-data';

This is incorrect. This sets the variable attr in this file as the default export of the ember-data package. If you want to import the thing named attr in the ember-data package, use curly braces, like:

import {attr} from 'ember-data';

However, looking at the guides, you should actually import from the ember-data/relationships package: like:

import {attr} from 'ember-data/relationships';

Though personally, I do:

import DS from 'ember-data';
export default DS.Model.extend({
  abc: DS.attr()

You can rename using this syntax:

import {attr as localNameForAttr} from 'ember-data/relationships';

export default Model.extend({
  abc: localNameForAttr()

Which in words sets the variable localNameForAttr in this file as the thing exported with name attr from the package ember-data/relationships.


Thanks kellen, that was it :slight_smile: