sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Table field name ambiguous error

Open andriy-polanski opened this issue 11 years ago • 5 comments

In a simple hasMany association, when using get[As] method, can we add table name or alias when specifying the foreign key?

For e.g.

 User.hasMany(Products);

user.getProducts() generates following query

 select Product.* from Product
 where UserId = [x]

I want to add table name 'Product' when specifying the UserId, something like

 select Product.* from Product
 where Product.UserId = [x]

Because, when trying to include another model which associates with User, I get field name ambiguous error for UserId

andriy-polanski avatar May 29 '15 12:05 andriy-polanski

Hmm odd, that should really happens on it own when using includes since it uses find internally.

Can you post a small reproduceable set of models association and calls that show this bug?

mickhansen avatar May 29 '15 12:05 mickhansen

@mickhansen

 User.hasMany(Products)
 User.hasMany(Comments)

 Products.hasMany(Comments)

 Comment.belongsTo(User)
 Comment.belongsTo(Product)


 user.getProducts({
      include: [
           { model: Comment, include: [ User ] }
      ]
  });

andriy-polanski avatar May 29 '15 12:05 andriy-polanski

@mickhansen Any thought?

andriy-polanski avatar May 29 '15 14:05 andriy-polanski

Haven't had a time to look at it. It should just work with that configuration.

mickhansen avatar May 29 '15 14:05 mickhansen

Can't reproduce this with latest master

janmeier avatar Aug 08 '15 10:08 janmeier