geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

"Cannot call method 'create' of undefined" trying to use "FamiliesUsers" as model / table #240

Closed OscarGodson closed 9 years ago

OscarGodson commented 9 years ago

Below is the code I'm trying to run (minus some obvious code, like require()s). It worked until I added the through associations. I have a table named families_users also.

User Model

var User = function () {
  this.defineProperties({
    // ...
  });

  this.hasMany('FamiliesUsers');
  this.hasMany('Families', {through: 'FamiliesUsers'});
};

User = model.register('User', User);

Family Model

var Family = function () {
  this.defineProperties({
    // ...
  });

  this.hasMany('FamiliesUsers');
  this.hasMany('Users', {through: 'FamiliesUsers'});
};

Family = model.register('Family', Family);

FamiliesUser Model

var FamiliesUser = function () {
  this.defineProperties({
    // ...
  });

  this.belongsTo('Families');
  this.belongsTo('Users');
};

FamiliesUser = model.register('FamiliesUser', FamiliesUser);

I changed FamiliesUser / families_users references and tables to Member / members and now I'm getting a "No inverse found for this through-association." which is a different error and I can figure that one out but means it must be something with the pluralization or two worded model maybe? Any ideas?

mde commented 9 years ago

Could you add the code that produced the error? Were you trying to save an item with a newly added association? We do have some good examples of through-associations in our tests:

https://github.com/geddy/model/tree/master/test/fixtures https://github.com/geddy/model/blob/master/test/integration/adapters/shared.js

Events and People are many-to-many through Participations, for example.

OscarGodson commented 9 years ago

The line of code that errors out is just:

          userModel.addFamily(FamilyModel.create({name: 'Your Family'}));

But, to be clear, the create above is not the thing that errors. It has to be something internal because typeof FamilyModel.create returns [Function]. So it's some internal Geddy create thing that is undefined.

I'll take a look at those fixtures.

mde commented 9 years ago

It can't find the model definition (constructor) for the join model (in your case FamiliesUser). It has to create an instance of that to store the link between the user and the family.

mde commented 9 years ago

Let me know what if anything you can find out.

OscarGodson commented 9 years ago

I responded with a small PR to hopefully help others :)

mde commented 9 years ago

Once you merge the PR, can you close this?