balderdashy / balderdashy/sails

Waterline incorrectly sees search for as value of 'OR' as the 'OR' operator in some cases.

Open
#6,977 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug orm resolved
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

**Node version**: 12.16.1
**Sails version** _(sails)_: 1.2.4
**ORM hook version** _(sails-hook-orm)_: 2.1.1
**Sockets hook version** _(sails-hook-sockets)_: n/a
**Organics hook version** _(sails-hook-organics)_: n/a
**Grunt hook version** _(sails-hook-grunt)_: n/a
**Uploads hook version** _(sails-hook-uploads)_: n/a
**DB adapter & version** _(e.g. sails-mysql@5.55.5)_: sails-postgres@1.0.2
**Skipper adapter & version** _(e.g. skipper-s3@5.55.5)_: n/a


There appears to be a bug in waterline that can in some cases cause it to misinterpret an 'OR' in a value as an operator.

```
patrick@admin:~/src/caremesh-directory$ yarn console-inspect --dontLift
yarn run v1.22.4
$ ./bin/console-inspect --dontLift
Debugger listening on ws://127.0.0.1:9229/1b139055-4650-4d0e-9ca9-46c8a481d044
For help, see: https://nodejs.org/en/docs/inspector

info: Loading app in interactive mode...
info: Sails is not listening for requests (since `dontLift` was enabled).
info: You still have access to your models, helpers, and `sails`.

Debugger attached.
info: Welcome to the Sails console.
info: ( to exit, type + )

(node:25985) [DEP0124] DeprecationWarning: REPLServer.rli is deprecated
sails> Address.count({city: 'PORTLAND', state: 'OR'}).log()Running with `.log()`...

- - - - - - - - - - - - - - - - - - - - - - - -
An error occurred:

Error [AdapterError]: Unexpected error from database adapter: The operator "undefined" is not permitted
at repl:1:9
at Script.runInThisContext (vm.js:120:20)
at REPLServer.defaultEval (repl.js:430:29)
at bound (domain.js:426:14)
at REPLServer.runBound [as eval] (domain.js:439:12)
at REPLServer.onLine (repl.js:758:10)
at REPLServer.emit (events.js:323:22)
at REPLServer.EventEmitter.emit (domain.js:482:12)
at REPLServer.Interface._onLine (readline.js:322:10)
at REPLServer.Interface._line (readline.js:699:8)
at REPLServer.Interface._ttyWrite (readline.js:1025:14)
at REPLServer.self._ttyWrite (repl.js:835:7)
at ReadStream.onkeypress (readline.js:198:10)
at ReadStream.emit (events.js:311:20)
at ReadStream.EventEmitter.emit (domain.js:482:12)
at emitKeys (internal/readline/utils.js:438:14)
at emitKeys.next ()
at ReadStream.onData (readline.js:1160:36)
at ReadStream.emit (events.js:311:20)
at ReadStream.EventEmitter.emit (domain.js:482:12)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at ReadStream.Readable.push (_stream_readable.js:209:10)
at TTY.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'AdapterError',
adapterMethodName: 'find',
modelIdentity: 'address',
raw: TypeError: The operator "undefined" is not permitted
at Formatter.operator (/home/patrick/src/caremesh-directory/node_modules/knex/lib/formatter.js:138:13)
at QueryCompiler_PG.whereBasic (/home/patrick/src/caremesh-directory/node_modules/knex/lib/query/compiler.js:525:100)
at QueryCompiler_PG.where (/home/patrick/src/caremesh-directory/node_modules/knex/lib/query/compiler.js:314:32)
at /home/patrick/src/caremesh-directory/node_modules/knex/lib/query/compiler.js:147:30
at Array.map ()
at QueryCompiler_PG.select (/home/patrick/src/caremesh-directory/node_modules/knex/lib/query/compiler.js:146:33)
at QueryCompiler_PG.toSQL (/home/patrick/src/caremesh-directory/node_modules/knex/lib/query/compiler.js:108:27)
at Builder.toSQL (/home/patrick/src/caremesh-directory/node_modules/knex/lib/query/builder.js:111:44)
at sequelizer (/home/patrick/src/caremesh-directory/node_modules/waterline-sql-builder/lib/sequelizer.js:1223:24)
at Object.generate (/home/patrick/src/caremesh-directory/node_modules/waterline-sql-builder/index.js:49:17)
at Object.compileStatement (/home/patrick/src/caremesh-directory/node_modules/machinepack-postgresql/machines/compile-statement.js:91:40)
at wrapper (/home/patrick/src/caremesh-directory/node_modules/@sailshq/lodash/lib/index.js:3282:19)
at Deferred.parley.retry [as _handleExec] (/home/patrick/src/caremesh-directory/node_modules/machine/lib/private/help-build-machine.js:1076:19)
at Deferred.exec (/home/patrick/src/caremesh-directory/node_modules/parley/lib/private/Deferred.js:286:10)
at Deferred.now (/home/patrick/src/caremesh-directory/node_modules/parley/lib/private/Deferred.js:592:8)
at Deferred.now (/home/patrick/src/caremesh-directory/node_modules/machine/lib/private/help-build-machine.js:1317:51)
at Deferred.execSync (/home/patrick/src/caremesh-directory/node_modules/machine/lib/private/help-build-machine.js:1530:23)
at Object.compileStatement (/home/patrick/src/caremesh-directory/node_modules/sails-postgresql/helpers/private/query/compile-statement.js:22:6)
at Object.select (/home/patrick/src/caremesh-directory/node_modules/sails-postgresql/helpers/select.js:127:37)
at wrapper (/home/patrick/src/caremesh-directory/node_modules/@sailshq/lodash/lib/index.js:3282:19)
at Deferred.parley.retry [as _handleExec] (/home/patrick/src/caremesh-directory/node_modules/machine/lib/private/help-build-machine.js:1076:19)
at Deferred.exec (/home/patrick/src/caremesh-directory/node_modules/parley/lib/private/Deferred.js:286:10)
at Deferred.switch (/home/patrick/src/caremesh-directory/node_modules/machine/lib/private/help-build-machine.js:1469:16)
at Object.find (/home/patrick/src/caremesh-directory/node_modules/sails-postgresql/lib/adapter.js:187:16)
at _getPopulatedRecords (/home/patrick/src/caremesh-directory/node_modules/waterline/lib/waterline/utils/query/help-find.js:145:21)
at helpFind (/home/patrick/src/caremesh-directory/node_modules/waterline/lib/waterline/utils/query/help-find.js:582:6)
at _afterPotentiallyRunningBeforeLC (/home/patrick/src/caremesh-directory/node_modules/waterline/lib/waterline/methods/find.js:258:9)
at _maybeRunBeforeLC (/home/patrick/src/caremesh-directory/node_modules/waterline/lib/waterline/methods/find.js:232:16)
at Deferred._.extend._WLModel [as _handleExec] (/home/patrick/src/caremesh-directory/node_modules/waterline/lib/waterline/methods/find.js:234:9)
at Deferred.exec (/home/patrick/src/caremesh-directory/node_modules/parley/lib/private/Deferred.js:286:10)
at Deferred.log (/home/patrick/src/caremesh-directory/node_modules/parley/lib/private/Deferred.js:652:8)
at repl:1:47
at Script.runInThisContext (vm.js:120:20)
at REPLServer.defaultEval (repl.js:430:29)
}
- - - - - - - - - - - - - - - - - - - - - - - -

undefined
sails>
```

This code works as expected when searching with only the state.

```
sails> Address.count({state: 'OR'}).log()
Running with `.log()`...
undefined
sails>
- - - - - - - - - - - - - - - - - - - - - - - -
Finished successfully.

Result:

41467
- - - - - - - - - - - - - - - - - - - - - - - -
```

It also works as expected when the state is not 'OR':

```
sails> Address.count({city: 'PORTLAND', state: 'ME'}).log()Running with `.log()`...

undefined
sails>
- - - - - - - - - - - - - - - - - - - - - - - -
Finished successfully.

Result:

1558
- - - - - - - - - - - - - - - - - - - - - - - -

sails>
```

I believe the problem is to be found in sequelizer.js, and might have something to do with options.strip, but I couldn't find anything describing how to use those parameters so I didn't want to go any further with it than that since I've never used knex directly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with Address.count({city: 'PORTLAND', state: 'OR'}) and compare it with the working state-only and non-OR searches. Inspect sequelizer.js, especially the options.strip handling mentioned in the report; done means the OR value remains a value and the combined query succeeds without the undefined-operator error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.