0

I know three ways to solve this problem.

  1. With a stored procedure.
  2. Ignoring the error and continue.
  3. Working an algorithm in backend

I would resolve with Node in my backend. So I did this.

let exist = false
connection.query(`
     SELECT IF(count(*) = 1, 'Exist','Not Exist') AS result
     FROM information_schema.columns
     WHERE table_schema = 'database'
       AND table_name = 'table_name'
       AND column_name = '${columnName}';
    `,
    (error, results, fields) => {
       if (error) throw error
       else if (results[0].result === 'Exist') {
         exist = true
       }
     }
)
if (!exist) {
   connection.query(`
      ALTER TABLE ofertas
      ADD COLUMN ${columnName} VARCHAR(255);
      `,
      (error, result, fields) => {
        if (error) throw error
      }
  )
}

This code not work. I belive the problem is async because exist always is true. I use mysql module

sebacc
  • 11
  • 5
  • What if you run the alter table statement as a callback to the check? – mmenschig Oct 21 '19 at 04:14
  • @mmenschig If i do that get Error: Cannot enqueue Query after invoking quit. code: 'PROTOCOL_ENQUEUE_AFTER_QUIT – sebacc Oct 21 '19 at 04:37
  • Are you calling `db.end()` anywhere in your code? try calling it at the end of your callback. Appears related to https://stackoverflow.com/questions/16134378/node-js-and-mysql-callback-query-in-query-callback – mmenschig Oct 21 '19 at 05:01

0 Answers0