typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.25k stars 6.31k forks source link

find operators broken in 0.2.21 #5209

Closed chriswep closed 4 years ago

chriswep commented 4 years ago

Issue type:

[ ] question [X] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [X] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

db.find(UserEntity, { 
   where: {
      userId: 1,
      age: MoreThan(30)
   }
})

the query log shows:

...
WHERE `UserEntity`.`userId` = ? AND `UserEntity`.`age` = ? -- PARAMETERS: [9549033,{"_type":"moreThan","_value":30,"_useParameter":true,"_multipleParameters":false}]

and the error:

  ...
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `_value` = 30, `_useParameter` = true, `_multipleParameters` = false, `useP' at line 1"
}

so apparently it just drops the find operator object into the sql query (since 0.2.21 - 0.2.20 works fine; only tested with MoreThan)

alexdavislws commented 4 years ago

Confirmed I have this issue as well with other operators.

EDIT: The operators work fine for when you are using getRepository() on its own, however they result in the above issue when using connection.getRepository() or manager.getRepository.

chriswep commented 4 years ago

good to know @alexdavislws - i already wondered why not more people reported this @pleerock what do you think?

matvi3nko commented 4 years ago

@chriswep because we are looking for problems in our code. I reviewed all the projects PRs and came to the conclusion that perhaps the problem is in TypeOrm.

const result = await repository.find( { where: whereClause }); doesn't work now

{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_value= 'xxxxxxxxxxxxxxxxxxxxx' at line 1

@chriswep Thanks for opening this! Hope it will be solved soon.

UPDATE: works fine on 0.2.20

chriswep commented 4 years ago

@matvi3nko @alexdavislws did you figure something out in the meantime? Maybe you can upvote the issue since it doesn't seem to get attention.

alexdavislws commented 4 years ago

@chriswep my team and I ended up going with the select QueryBuilder. We just use the operators in generic SQL WHERE clause string (with :params) and pass it to the .where() methods.

ex:

const data = getRepository('some_table')
      .createQueryBuilder('some_alias')
      .select('some_alias')
      .where('some_alias.col1 > :param1', { param1: 0 })
      .getMany();
chriswep commented 4 years ago

@matvi3nko @alexdavislws i tried updating to 0.2.24 and i can't reproduce the bug there. however i'm not sure if this is due to some other changes in my code and/or other dependencies - this issue doesn't seem to be addressed somewhere in the typeorm changelog. Can you confirm the issue is gone in the current version? If yes, we could close this issue.

BIGRUSSIANBOSS commented 4 years ago

@chriswep Hi, not work for In operator. Try to check - find({ where: { field: In(array) } }) It also drops find operator MySQL only, I think this is the same bug

chriswep commented 4 years ago

the issue is most likely the same as in https://github.com/typeorm/typeorm/issues/6144 - having multiple typeorm versions in the same project.