Skip to content

Raw SQL Query - Parameter Not Working #881

@mikegani

Description

@mikegani

Hi guys,

I'm hoping you guys can help me out. I'm trying to use the raw SQL query function on TypeORM but failed when binding the parameter.

I have tried both getRepository and entityManager, they both yield the exact same error message.

Background:
I'm running Node.js version v7.4.0, and the typeORM version 0.1.0-alpha.35
I'm using MySQL database and the database connection is good.

This is the successful code (without parameter binding)

const result = await db.entityManager.query("select id, email, name from users where id=1")
  .catch(err_msg => {
      console.log("error happen during query");
      console.log(err_msg);
  });

This is the failing code (with parameter binding):

const result = await db.entityManager.query("select id, email, name from users where id=:PARAMID, [1]")
  .catch(err_msg => {
      console.log("error happen during query");
      console.log(err_msg);
  });

Here is the error message (with parameter binding):

error happen during query
{ QueryFailedError: 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 ':PARAMID' at line 1
at new QueryFailedError (/node_modules/typeorm/error/QueryFailedError.js:27:28)
at Query._callback (/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:197:45)
at Query.Sequence.end (/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
at Query.ErrorPacket (node_modules/mysql/lib/protocol/sequences/Query.js:90:8)
at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket. (/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
code: 'ER_PARSE_ERROR',
errno: 1064,
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 ':PARAMID' at line 1',
sqlState: '42000',
index: 0,
sql: 'select id, email, name from profiles where id= :PARAMID',
name: 'QueryFailedError',
query: 'select id, email, name from profiles where id= :PARAMID',
parameters: [ 1 ] }

Any idea / feedback is greatly appreciated.

Thank you,

Michael

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions