Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize.fn(Sequelize.col(...)) IN (...) in where condition? #2077

Closed
djmadcat opened this issue Jul 22, 2014 · 8 comments
Closed

Sequelize.fn(Sequelize.col(...)) IN (...) in where condition? #2077

djmadcat opened this issue Jul 22, 2014 · 8 comments
Assignees

Comments

@djmadcat
Copy link

Can't find is it possible to add something like
COALESCE(place_type_codename, announcement_type_codename) IN ('Lost', 'Found')
to where condition?
i.e.:

SELECT
    id, type, item_id, place_type_codename, announcement_type_codename
FROM
    feed
WHERE
    archived_at IS NULL
    AND
    COALESCE(place_type_codename, announcement_type_codename) IN ('Lost', 'Found')

I know we can do something like

var filter = ['Lost', 'Found'];
Sequelize.and(
    { archived_at: null },
    Sequelize.or({
      place_type_codename: filter
    }, {
      announcement_type_codename: filter
    })

but let's assume that we have another complex function.

Thanks.

@mickhansen
Copy link
Contributor

This is an ideal case for sequelize.where(sequelize.fn(), {in: []}) however that is not fully supported yet.
For now you might have to use sequelize.literal() and write it raw.

@djmadcat
Copy link
Author

@mickhansen Thanks.
I will appreciate if you give snippets of using:

  • sequelize.where(sequelize.fn(), {in: []}) with sequelize.and (is it possible?)
  • sequelize.literal with sequelize.and

in findAll request.

@jtraband
Copy link

@mickhansen
I'm trying to do something similar with

  var fn = Sequelize.fn("UPPER", Sequelize.col("CompanyName"));     
  var where = Sequelize.where(fn, {like: 'B%'});
  customerModel.findAll( where ).then(function(r) { ... });

But am unable to get it to work. Am I missing something, or is there a workaround using sequelize.literal that will accomplish the same thing. Interestingly the query executes but doesn't perform any filtering at all.

@janmeier
Copy link
Member

@jtraband

return Company.findAll({
    where: Sequelize.where(
      Sequelize.fn("UPPER", Sequelize.col("CompanyName")),
      { like: 'B%' }
    )
  });

Works fine for me on latest master

SELECT `id`, `CompanyName` FROM `companies` AS `company` WHERE UPPER(`CompanyName`) LIKE 'B%';

@jtraband
Copy link

Thanks, is there any to get this version from npm or do I need to clone the latest git and link it locally. The 2.0.0 rc version i just got from npm does not work.

@janmeier
Copy link
Member

npm install sequelize/sequelize

@janmeier janmeier self-assigned this Nov 28, 2014
@janmeier
Copy link
Member

@djmadcat Finally fixed :) - See the test in the referenced commit for the required syntax

@fahrenq
Copy link

fahrenq commented Dec 17, 2018

As a note, to combine .fn-properties and other properties - just use an array for where key.

      where: [
        sequelize.where(
          sequelize.fn('array_length', sequelize.col('patterns'), 1),
          { [Op.gt]: 0 },
        ),
        { companyId: this.companyId },
      ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants