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

3
  • What if you run the alter table statement as a callback to the check? Commented Oct 21, 2019 at 4:14
  • @mmenschig If i do that get Error: Cannot enqueue Query after invoking quit. code: 'PROTOCOL_ENQUEUE_AFTER_QUIT Commented Oct 21, 2019 at 4:37
  • Are you calling db.end() anywhere in your code? try calling it at the end of your callback. Appears related to stackoverflow.com/questions/16134378/… Commented Oct 21, 2019 at 5:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.