node.js - Sequelize produces invalid query "model.id AS model.id" -
here sequelize call i'm making count favorites along model:
model.findall({ group: [ 'model.id', 'favorites.id' ], attributes: [ '*', [ sequelize.fn('count', sequelize.col('favorites.id')), 'favorites_count' ] ], include: [ { attributes: [], model: favorite }, ] });
here's query produces:
select model.id, model.*, count(favorites.id) favorites_count, favorites.id favorites.id # invalid! models model ...
here's error
error: syntax error @ or near "." line 1: select model.*, favorites.id favorites.id, ... ^
because favorites.id
not valid alias. why sequelize produce invalid alias , how prevent it?
you cannot prevent - sequelize needs alias column. query becomes invalid because have set quoteidentifiers
false. query sequelize wants "favorites"."id" "favorites.id"
why sequelize doing such "stupid" thing might say. lets take example:
select "model"."id", "favorites"."id" ...
this produces following result set:
[{ id: 42 }]
because 2 columns have same name, override each other 1 of columns returned. need alias favorites column get
[{ id: 42, 'favorites.id': 87 }]
Comments
Post a Comment