0

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');
    });
  }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1) assuming you use FB 2.5 or later, i would suggest using TraceAPI tools to see which queries FB server actually receives, not which queries an application thinks it should receive. http://FBProfiler.sf.net or IBExpert PErsonal should do, or any other. 2) i **suspect** you have a mismatch between column charset and connection charset and actual data. Which leads to UTF-8 bytestream being tried as MBCS bytestream, or vice versa. – Arioch 'The Feb 04 '21 at 07:26
  • Check the charset of the column in question and also check the charset of the connection itself. See the "comments" tab in the right panel for general discussion and also for handy diagnosing queries - https://topanswers.xyz/databases?q=1462 // also please, don't do string splicing!! https://www.bobby-tables.com the data should be sent isolated from the query source text, as parameters! this would make them less fragile in both datatypes sense and injection security sense too – Arioch 'The Feb 04 '21 at 07:26
  • Please post textual error output as **text** in your question, please do not use screenshots. – Mark Rotteveel Feb 04 '21 at 14:30
  • Curious, I would sooner expect an error for your attempt to insert `'1612387572153'` into a column of type `TIMESTAMP`. A `TIMESTAMP` expects a datetime value, eg `'2021-02-04 15:31'`. Also, when inserting, it is recommend to explicitly specify the column list, just in case the number or order of columns in your table do not match your expectation. – Mark Rotteveel Feb 04 '21 at 14:34
  • @Arioch'The It is unlikely that `'pending'` would trigger a conversion error for any single byte or UTF8 character set (as all those characters are in US_ASCII, and thus in most - if not all single byte character sets, and in UTF8), the only exception would possibly for one of the 2bpc character sets like BIG5 – Mark Rotteveel Feb 04 '21 at 14:38
  • Issue is fixed when I specify the column list during insertion. Thanks, @Mark Rotteveel. – Blade Wolfmoon Feb 04 '21 at 16:15
  • That would mean that the `CREATE TABLE` statement in your question does not match what was actually used to create the table in the database, and the columns are in a different order. – Mark Rotteveel Feb 04 '21 at 16:32
  • @MarkRotteveel correct, i made a huge overshot re: UTF-8 – Arioch 'The Feb 05 '21 at 05:15

1 Answers1

0

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