question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sequelize.col() not recognizing attribute alias

See original GitHub issue

What 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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
leandrocostaoliveiracommented, Aug 8, 2019

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

1reaction
wangzc93commented, Jul 23, 2019

@TakanashiOuken See my “What I have tried” section above.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found