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.

SQL Syntax error when empty array is given into "In" method

See original GitHub issue

Issue type: bug report

Database system/driver: mysql / mariadb

TypeORM version: 0.2.5

example code:

import { In } from 'typeorm'
// ...
const res = await Entity.find({ where: { id: In([]) } })
// ER_PARSE_ERROR: You have an error in your SQL syntax; ...

Generated SQL is similar to

SELECT * FROM users WHERE id IN();

In MySql IN () (empty IN) is a syntax error. To fix it i propose that either:

  • improve type annotations not to allow empty arrays to be inserted inside In method with guard for checking array.length to be > 0 (this would be closest to mysql spec)
  • In method converts empty array to FALSE so the result would be similar to
SELECT * FROM users WHERE id IN(FALSE); -- FALSE seems to work if used also in NOT IN(FALSE)
  • remove IN() clause (may or may not create other sql syntax errors)
SELECT * FROM users WHERE id;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

40reactions
Gindencommented, Aug 27, 2019

There is workaround. You can do:

const res = await Entity.find({ where: { id: In([null, ...ids]) } })

X IN (NULL) is equivalent to 1=0, and X IN (NULL, 3) is equivalent to X IN (3)

11reactions
bogdancommented, Dec 9, 2019

IMO the way typeorm treats In(null) is counter intuitive to people who are not 10 years in the SQL world. Ideally I would want ORM to understand what I mean like so:

find({where: {key: [null, 1, 2]}) // => where key is null or key in (1,2)
find({where: {key: [undefined]}) // => where key is null
find({where: {key: []) // => where 1 = 0
Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL IN Operator ERROR when array is empty - Stack Overflow
1 Answer 1 ... You can check if the array is empty: if(!empty($city)) { $query = "SELECT * FROM Customers WHERE City IN...
Read more >
15: 38.5. Query Language (SQL) Functions - PostgreSQL
SQL functions execute an arbitrary list of SQL statements, returning the result of the last query in the list. In the simple (non-set)...
Read more >
(The only proper) PDO tutorial - Treating PHP Delusions
PDOStatement::fetchAll() returns an array that consists of all the rows returned by the query. From this fact we can make two conclusions: This...
Read more >
Everything you wanted to know about arrays - PowerShell
An empty array can be created by using @(). PowerShell ... PS> $empty = $null PS> $empty[0] Error: Cannot index into a null...
Read more >
Work with arrays | BigQuery - Google Cloud
With Google Standard SQL, you can construct array literals, build arrays from subqueries using the ARRAY function, and aggregate values into an array...
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