SQL Syntax error when empty array is given into "In" method
See original GitHub issueIssue 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 toFALSE
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:
- Created 5 years ago
- Reactions:14
- Comments:28 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is workaround. You can do:
X IN (NULL)
is equivalent to1=0
, andX IN (NULL, 3)
is equivalent toX IN (3)
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: