sequelize: Associations where model name clashes with a property already defined on the model should throw an error

Update by janmeier The error turned out to be a naming conflict between an attribute and the name of the associated model. An error should be thrown when the association is created if there is a conflict

Dear developers

I was running my application on a development server and everything went fine. Then I moved everything to the production server but something went wrong there. In my package.json file I had “sequelize”: “latest” and it downloaded the 2.0.0 beta version. Not sure why that’s the default version now, but ok. I removed that version and installed version 1.7.0-rc2 but it seems that it does not solve my problem. The problem is the relationships of my models. Some does not seem to work.

An example that does not work for me. I have a table Person and a table Car. A person has multiple cars, and a car belongs to a person. So far so good. The relationships are defined as such:

Person.hasMany(Car, {foreignKey: 'person'});
Car.belongsTo(Person, {foreignKey: 'person'});

If I retrieve all the persons and include the cars, it works fine.

Person.findAll({include: [Car]}).success(function(persons) {
    // Do whatever you want
});

But when I ask it the other way around, it does not work.

Car.findAll({include: [Person]}).success(function(cars) {
     // Do whatever you want
});

The query that get’s executed is:

SELECT `car`.*, `person`.`ID` as `person.ID`, `person`.`name` as `person.name` FROM `car` LEFT OUTER JOIN `person` as `person` ON `person`.`ID`=`car`.`person`;

So it returns a table with the following attributes: ID, make, color, person, person.ID, person.name but when I stringify my response from sequelize, the only thing I get is {ID, make, color}. It seems that the rest of the person date is omitted or something.

Thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 26 (15 by maintainers)

Most upvoted comments

I don’t see how it could be “solved”. Naming conflicts are just that. Perhaps you can work around it by using the as option.

Car.belongsTo(Person, {as: 'PersonModel', foreignKey: 'person'});