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.where function not working in model scope

See original GitHub issue

I am trying to use a sequelize.where function in one of the scopes in one of my models. However, when I then do a query with that scope, the query gets mangled.

What you are doing?

Run node test.js to get the result.

models.js

module.exports = function(sequelize, DataTypes) {
  const Store = sequelize.define('store', {
    name: {
      type: DataTypes.STRING,
      validate: {
        notEmpty: true
      }
    },
    location: DataTypes.GEOGRAPHY,
  }, {
    scopes: {
      distance: function(location, radius){
        return {
          where: sequelize.where(
            sequelize.fn(
              'ST_Distance',
              sequelize.col('store.location'),
              sequelize.fn(
                'ST_GEOGFROMTEXT',
                'POINT(' + location.longitude + ' ' + location.latitude + ')'
              )
            ),
            '<=',
            radius
          )
        };
      }
    }
  });

  return {
    Store: Store
  };
};

test.js

const Sequelize = require('sequelize');
let credentials = require('./credentials');

const db = new Sequelize(credentials.TEST_DATABASE_URL, {
  logging: true
});
const models = db.import(__dirname + '/models');
let location = {latitude: 51.04, longitude: 36.09};
let radius = 5;
models.Store.scope([{
  method: [
    'distance',
    location,
    radius
  ]
}]).findAll()
.then(function(result){
  console.log(result);//just logging the result
});

What do you expect to happen?

The query to execute without errors and pull all stores that are less than or equal to 5 meters from the provided latitude and longitude. Since this code sample doesn’t place any stores in the db, I just expect this code sample to execute without error.

What is actually happening?

The query executes with the error SequelizeDatabaseError: column store.attribute does not exist

Output: The query that runs follows: SELECT "id", "name", "location", "createdAt", "updatedAt", "userId" FROM "stores" AS "store" WHERE "store"."attribute" = ST_Distance("store"."location", ST_GEOGFROMTEXT('POINT(36.09 51.04)')) AND "store"."comparator" = '<=' AND "store"."logic" = 5;

Dialect: postgres Sequelize version: 3.23.6

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
felixfbeckercommented, Oct 28, 2016

Please don’t +1 issues. It clutters the thread without adding value to the discussion and spams maintainers with notifications. Use GitHub reactions to upvote features. image

1reaction
tumdavcommented, May 7, 2018

Same

Read more comments on GitHub >

github_iconTop Results From Across the Web

include - Sequelize : Scope with where condition not working ...
The best way is to force sequelize.js to use the original names mentioned in model while creating tables in database you can make ......
Read more >
Scopes - Sequelize
Scopes are defined in the model definition and can be finder objects, or functions returning finder objects - except for the default scope,...
Read more >
sequelize query use column name instead model name
So I define a model based on how it's listed in the docs. This worked when I was on Sequelize 4.* and Sequelize-Typescript...
Read more >
The Comprehensive Sequelize Cheatsheet
Sequelize is the most famous Node ORM and is quite feature-rich, but while using it I spend much of m... Tagged with database,...
Read more >
Using Sequelize ORM with Node.js and Express - Stack Abuse
Model class and running the .init() function, passing parameters, or by defining a const and assigning it the returned value of the .define() ......
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