@mysql/xdevapi How to debug "wrong number of fields in row being inserted" in nodejs?

142 views Asked by At

I successfully connected to a local MySQL server using the new x-authentication. I have a products table with the following schema (DDL):

CREATE TABLE `products` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `description_he` text,
  `description_en` text,
  `display_name_he` varchar(45) DEFAULT NULL,
  `display_name_en` varchar(45) DEFAULT NULL,
  `image_path` varchar(255) DEFAULT NULL,
  `price` smallint unsigned NOT NULL,
  `is_visible` tinyint NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='contains information about products'

I tried to insert the following javascript object into this table:

Product {
  productID: null,
  productName: 'product2',
  descriptionHE: 'תיאור המוצר',
  descriptionEN: 'product description',
  displayNameHE: 'מוצר',
  displayNameEN: 'Product',
  imagePath: 'assets/images/facebook-512.png',
  price: 400
}

I did this using the following code:

let table = this.session.getSchema('...').getTable('products')
return table.insert(
  'id', 'name', 'description_he', 'description_en', 'display_name_he', 'display_name_en', 'image_path', 'price', 'is_visible')
    .values(
      5,
      product.name,
      product.descriptionHE,
      product.descriptionEN,
      product.displayNameHE,
      product.displayNameEN,
      product.imagePath,
      product.price,
      1)
    .execute()
    .catch((err) => {
      console.log(err);
    })

This catches the following error:

Error: Wrong number of fields in row being inserted
    at SqlResultHandler.BaseHandler.<computed> (C:\Users\...\node_modules\@mysql\xdevapi\lib\Protocol\InboundHandlers\BaseHandler.js:119:17)
    at Array.entry (C:\Users\...\node_modules\@mysql\xdevapi\lib\Protocol\InboundHandlers\BaseHandler.js:90:29)
    at WorkQueue.process (C:\Users\...\node_modules\@mysql\xdevapi\lib\WorkQueue.js:75:19)
    at Client.handleServerMessage (C:\Users\...\node_modules\@mysql\xdevapi\lib\Protocol\Client.js:208:21)
    at Client.handleNetworkFragment (C:\Users\...\node_modules\@mysql\xdevapi\lib\Protocol\Client.js:252:14)
    at TLSSocket.<anonymous> (C:\Users\...\node_modules\@mysql\xdevapi\lib\Protocol\Client.js:90:36)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
  info: {
    severity: 0,
    code: 5014,
    sqlState: 'HY000',
    msg: 'Wrong number of fields in row being inserted'
  }
}

I tried playing with the arguments and simplifying the table schema a bit but I can't find the error yet. Do you know how can I debug the query sent to the SQL server to figure out why it's failing?

1

There are 1 answers

0
Matan Liram On

The problem was that I used product.name instead of product.productName. Conclusion, use typescript. :)