Skip to content

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

@tafkanator

Description

@tafkanator

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;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions