Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Consulta Knex RAW MySQL agregando nuevas líneas

Soy nuevo en MySQL y anteriormente usé PSQL de la misma manera que estoy usando MySQL, pero el siguiente código tiene errores.

 return await db .raw( `INSERT INTO users(firstName, lastName, email, password, type, addressLine1, addressLine2, addressLine3, mobileNumber, townCity, postcode) VALUES ( '${firstName}', '${lastName}', '${email}', '${hashedPassword}', '${mobileNumber}', ${type}, '${addressLine1}', '${addressLine2}', '${addressLine3}', '${townCity}', '${postcode}'); SELECT * from users WHERE id = LAST_INSERT_ID()`, ) .then(() => ...)

Devolviendo el siguiente 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 'SELECT * from users WHERE id = LAST_INSERT_ID()' at line 24", sql: 'INSERT INTO users(firstName, \n' + ' lastName, \n' + ' email, \n' + ' password, \n' + ' type,\n' + ' addressLine1, \n' + ' addressLine2, \n' + ' addressLine3, \n' + ' mobileNumber,\n' + ' townCity, \n' + ' postcode)\n' + ' VALUES (\n' + " 'Jane',\n" + " 'Doe',\n" + " 'janedoe@email.com',\n" + " 'password', \n" + " '07123456789',\n" + ' 100, \n' + " '1', \n" + " 'Test road', \n" + " 'Westminster', \n" + " 'London', \n" + " 'L1 1TG'); \n" + ' SELECT * from users WHERE id = LAST_INSERT_ID()' }

Sospecho que está relacionado con los literales de la plantilla, pero no estoy seguro de cuál es la mejor manera de resolver esto, funcionó bien en PSQL (excluyendo la última línea). ¿Hay alguna forma mejor de usar SQL sin procesar? No estoy interesado en todos los comandos ORM de alto nivel.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Parece que no puedes hacer demasiado en una consulta. Extraer SELECT * from users WHERE id = LAST_INSERT_ID() en su propia consulta después de que la inserción funcionó bien

about 4 years ago · Juan Pablo Isaza Report

0

Necesitas habilitar multipleStatements

 const mysql = require('mysql') connection = mysql.createConnection({ host: 'localhost', user: 'youruser', password: 'yourpassword', database: 'yourdb', multipleStatements: true }) connection.connect() connection.query('INSERT INTO users(firstName, lastName, email, password, type, addressLine1, addressLine2, addressLine3, mobileNumber, townCity, postcode) VALUES ( '${firstName}', '${lastName}', '${email}', '${hashedPassword}', '${mobileNumber}', ${type}, '${addressLine1}', '${addressLine2}', '${addressLine3}', '${townCity}', '${postcode}'); SELECT * from users WHERE id = LAST_INSERT_ID();', function(err, results) { if (err) { throw err } console.log(results[0]]) }) connection.end()
about 4 years ago · Juan Pablo Isaza Report

0

Intente ejecutar la primera consulta por separado. Si no se ejecutó, verifique la estructura de su tabla. Parece que a la estructura le falta la configuración de la clave principal o algo en su migración

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!