New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parametized query wont work #556
Comments
When using await getEntityManager().query('SELECT * FROM tbl_1 WHERE name = ?', [ p_name ]) |
maybe i confused it with different driver. as i told it driver-specific thing |
Well, that's fine then, i got it working now thanks a lot! |
For future reference, it seems one could currently use the built-in Like: // For PostgreSQL yields:
// query: select a, b from my_table where x between $1 and $2
// parameters: [ 2, 10 ]
const [query, parameters] = conn.driver.escapeQueryWithParameters(
'select a, b from my_table where x between :low and :high',
{ high: 10, low: 2 }
);
conn.manager.query(query, parameters).then(...); Of course, this does not seem to be in the public API, so might change in future releases. |
What about arrays?
|
Apparently, this works (in Postgres):
|
Had similar issues and needed to use another format to handle ILIKE thought I would share in case it helps any TypeOrm (ers) |
The trick with Alternatively, is there any other clean (non-driver-dependent) way to properly escape query parameters when using a raw query (not // sorry for using an old issue to ask a question, but people will keep arriving here from a google search (like me), so it kinda feels in place :) EDIT: after some more research (thx @akwodkiewicz) in the driver code, we found out that for this usecase |
If you're using mysql, you can set namedPlaceholders to true (it is false by default) in your configuration options:
https://github.com/sidorares/node-mysql2/blob/master/documentation/Extras.md#named-placeholders |
Based on comments above (#556 (comment) and #556 (comment)), we now use the following utility in migrations: const queryWithParams = (
queryRunner: QueryRunner,
sql: string,
params: ObjectLiteral
): ReturnType<QueryRunner['query']> =>
queryRunner.query(...queryRunner.connection.driver.escapeQueryWithParameters(sql, params, {})); Thanks! |
I got an error when running this code: |
Hi!
Im using typescript-express-example template and i was successfully connected my application to my MySQL database.
Now i am working with entityManager.query to insert, select etc.. some data.
Here is my errors when running this:
Another error is when running this:
But when using a direct plain text:
const result = await getEntityManager().query('SELECT * FROM tbl_1 WHERE name = 'some_name';')
It works by this way.
Any idea how to resolve this errors?
The text was updated successfully, but these errors were encountered: