Search code examples
node.jstype-conversionfirebirdnode-firebird

How to fix [Error: While query - conversion error from string "pending" ] when trying to insert a new record into firebird database?


I've created a table with the following schema:

CREATE TABLE orders (
    id INTEGER NOT NULL PRIMARY KEY,
    status VARCHAR(30) NOT NULL CHECK(status IN('ordered', 'paid', 'pending', 'complete')),
    order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    delivery_id INTEGER,
    client_id INTEGER,
    operator_id INTEGER,
    FOREIGN KEY(delivery_id)
    REFERENCES delivery_info(id) ON DELETE CASCADE,
    FOREIGN KEY(client_id) REFERENCES client(id) ON DELETE CASCADE,
    FOREIGN KEY(operator_id) REFERENCES operator(id)
);

However, when I'm trying to insert new data into the table using a node.js application, I get the following error: Console output

Generated query looks okay to me:

INSERT INTO orders VALUES (793771, 'pending', '1612387572153', 590931, 3923, 0);

Application code:

class Order {
  static DEFAULT_OPERATOR_ID = 0;

  constructor(id, status, time, delivery, client,
              operatorId = Order.DEFAULT_OPERATOR_ID)
  {
    this.id = id;
    this.status = status;
    this.time = time;
    this.delivery = delivery;
    this.client = client;
    this.operatorId = operatorId
  }

  save() {
    console.log(this);
    const insertQuery = `
        INSERT INTO orders
        VALUES (${this.id}, '${this.status}', '${this.time}', ${this.delivery.id}, ${this.client.id}, ${this.operatorId});
    `;

    console.log(insertQuery);
    dbConnection.query(insertQuery, (error, result) => {
      if (error) throw error;
      console.log('Inserted new order');
    });
  }
}

Solution

  • Issue is fixed when I specify the column list during insertion. Thanks, @Mark Rotteveel.