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

Postgres $any with an array gets a sytax error #4064

Closed
joelanman opened this issue Jul 5, 2015 · 6 comments · Fixed by #5840
Closed

Postgres $any with an array gets a sytax error #4064

joelanman opened this issue Jul 5, 2015 · 6 comments · Fixed by #5840
Labels
dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). type: bug

Comments

@joelanman
Copy link

If I construct a WHERE clause with $any and an array, I get an error.

var selectedAnimals = ['cow','pig'];

where.title = {
    $iLike: { $any: selectedAnimals}
};

generates this SQL:

SELECT "name" FROM "Animals" AS "Animal" WHERE "Animal"."type" ILIKE ANY ARRAY['cow','pig']::VARCHAR;

and this error:

syntax error at or near "ARRAY"

From Postgres syntax, it seems like ANY should have brackets, like this:

SELECT "name" FROM "Animals" AS "Animal" WHERE "Animal"."type" ILIKE ANY (ARRAY['cow','pig'])
@janmeier
Copy link
Member

janmeier commented Jul 6, 2015

Hmm, from the unit tests, it looks like we should be generating with brackets. Have you tried latest master

suite('$any', function() {

@corbanb
Copy link

corbanb commented Jul 15, 2015

+1 I am getting this in 3.3.2

SELECT "id", "protocol", "password_encrypted", "provider", "identifier", "friends", "tokens", "accessToken", "createdAt", "updatedAt", "UserId" FROM "Passports" AS "Passport" WHERE "Passport"."identifier" = ANY (ARRAY['333333333333']::VARCHAR) AND "Passport"."provider" = 'facebook';

Removing ::VARCHAR allows this query to work also.

@corbanb
Copy link

corbanb commented Jul 15, 2015

@mickhansen @janmeier I updated to the bleeding edge. same bug in master.

@janmeier janmeier added type: bug dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). labels Oct 12, 2015
@vhmth
Copy link

vhmth commented Feb 24, 2016

Reproducable.

error looking up relevant campaigns: { [SequelizeDatabaseError: syntax error at or near "ARRAY"]
  name: 'SequelizeDatabaseError',
  message: 'syntax error at or near "ARRAY"',
  parent:
   { [error: syntax error at or near "ARRAY"]
     name: 'error',
     length: 88,
     severity: 'ERROR',
     code: '42601',
     detail: undefined,
     hint: undefined,
     position: '206',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     schema: undefined,
     table: undefined,
     column: undefined,
     dataType: undefined,
     constraint: undefined,
     file: 'scan.l',
     line: '1053',
     routine: 'scanner_yyerror',
     sql: 'SELECT "id", "client_id", "max_sessions", "name", "url", "complete", "questions", "createdAt", "updatedAt" FROM "campaigns" AS "Campaign" WHERE "Campaign"."complete" = false AND "Campaign"."url" ILIKE ANY ARRAY[\'http://opentest.co%\',\'https://opentest.co%\',\'http://www.opentest.co%\',\'https://www.opentest.co%\']::TEXT ORDER BY "Campaign"."createdAt" ASC;' },
  original:
   { [error: syntax error at or near "ARRAY"]
     name: 'error',
     length: 88,
     severity: 'ERROR',
     code: '42601',
     detail: undefined,
     hint: undefined,
     position: '206',
     internalPosition: undefined,
     internalQuery: undefined,
     where: undefined,
     schema: undefined,
     table: undefined,
     column: undefined,
     dataType: undefined,
     constraint: undefined,
     file: 'scan.l',
     line: '1053',
     routine: 'scanner_yyerror',
     sql: 'SELECT "id", "client_id", "max_sessions", "name", "url", "complete", "questions", "createdAt", "updatedAt" FROM "campaigns" AS "Campaign" WHERE "Campaign"."complete" = false AND "Campaign"."url" ILIKE ANY ARRAY[\'http://opentest.co%\',\'https://opentest.co%\',\'http://www.opentest.co%\',\'https://www.opentest.co%\']::TEXT ORDER BY "Campaign"."createdAt" ASC;' },
  sql: 'SELECT "id", "client_id", "max_sessions", "name", "url", "complete", "questions", "createdAt", "updatedAt" FROM "campaigns" AS "Campaign" WHERE "Campaign"."complete" = false AND "Campaign"."url" ILIKE ANY ARRAY[\'http://opentest.co%\',\'https://opentest.co%\',\'http://www.opentest.co%\',\'https://www.opentest.co%\']::TEXT ORDER BY "Campaign"."createdAt" ASC;' }

@igio
Copy link

igio commented Mar 31, 2016

I'm still running into the same issue as of version 3.20.0, installed from npm.

While @janmeier - is correct for $any tests, the tests targeting the issue here, combination of $ilike, $any and array, (https://github.com/sequelize/sequelize/blob/master/test/unit/sql/where.test.js#L569) expects the array statement without the parentheses, generating an sql statement which throws an error when run against the database.

@serranoarevalo
Copy link

I managed to make it work by including a sequelize literar after the iLike.

const tagsArray = ['foo', 'bar']

Images.findAll({
where: {
    tags: {
        $iLike: Sequelize.literal(`ANY(ARRAY[${tagsArray}])`)
    }
}
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants