sequelize.col() not recognizing attribute alias
See original GitHub issueWhat are you doing?
I map the attribute alias_a
to column a
in database:
class Test extends Model {}
Test.init({
alias_a: { type: Sequelize.INTEGER, field: 'a' },
},{
sequelize,
});
But when querying with sequelize.col
, it can not recognize the alias I defined in Model.
Test.findAll({
attributes: [
[sequelize.fn('SUM', sequelize.col('alias_a')), 'sum_a'],
],
});
I know Test.sum('alias_a')
can recognize the alias defined in Model, but sometimes I need to use findAll
to get other attributes at the same time.
What do you expect to happen?
{ sum_a: 123 }
What is actually happening?
SequelizeDatabaseError: Unknown column ‘alias_a’ in ‘field list’
What I have tried
I tried to solve this issue by adding a static method col
to class Model
:
class _Model extends Model {
static col(attribute) {
const attrOptions = this.rawAttributes[attribute];
const field = (attrOptions && attrOptions.field) || attribute;
return this.sequelize.col(field);
// above 3 lines are copied from Model.aggregate method
}
}
Then I can do like:
Test.findAll({
attributes: [
[sequelize.fn('SUM', Test.col('alias_a')), 'sum_a'],
],
});
which works fine. Just wondering are there any better ways to handle this issue?
Dialect: any Sequelize version: 5.2.13 Tested with latest release: Yes
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Error when utilizing column aliases in where conditions for ...
Unfortunately I seem to be unable to make use of the aliased attributes when constructing where conditions in Sequelize findAll(), findOne() ...
Read more >Model Querying - Basics - Sequelize
When using aggregation function, you must give it an alias to be able to access it from the model. In the example above...
Read more >Getters, Setters & Virtuals - Sequelize
Sequelize allows you to define custom getters and setters for the attributes of your models.
Read more >Naming Strategies - Sequelize
Without the underscored option, Sequelize would automatically define: A createdAt attribute for each model, pointing to a column named createdAt in each table ......
Read more >Eager Loading - Sequelize
If an association is aliased (using the as option), you must specify this alias when including the model. Instead of passing the model...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I supose the code used to work around the issue should be moved into the Sequelize.col function, making the process transparent. I used some similar approach in the past with Sequelize 4.x, when updated to 5.x, the inner names seems to have changed and my software broke. Anyway, just leaving my +1 to this issue. By now, I will use the approuch in “What I have tried”, thanks @wangzc93
@TakanashiOuken See my “What I have tried” section above.