Issue storing JSON


#1

I’m using MySQL 5.6 and trying to store JSON in a text field but the generated SQL is a tad squiffy. My code reads

let log = {
	order_id: modelInstance.id,
	user_id: user_id,
	name: username,
	type: 'Updated',
	item_changed: 'Order:' + modelInstance.id,
	changes: modelInstance.toJSON()
}

await Log.create( log )

The generated SQL reads as follows and it would appear we are parsing the modelInstance data passed as JSON.

sql: 'insert into `logs` (`changes`, `created_at`, `item_changed`, `name`, `order_id`, `type`, `updated_at`, `user_id`) values (`id` = 3, `number` = \'717206\', `channel` = \'NOP\', `dated_at` = \'2018-10-30T09:33:00.000Z\', `ip_address` = \'99.44.109.157\', `status` = \'Cancelled\', `base_subtotal_including_tax` = 71.16, `base_subtotal_excluding_tax` = 59.3, `base_shipping_including_tax` = 498.66, `base_shipping_excluding_tax` = 415.55, `base_tax` = 94.97, `base_total` = 569.82, `base_discount` = 0, `subtotal_including_tax` = 78.28, `subtotal_excluding_tax` = 65.23, `shipping_including_tax` = 548.53, `shipping_excluding_tax` = 457.11, `tax` = 104.47, `total` = 626.8, `discount` = 0, `currency_code` = \'GBP\', `problem` = \'1\', `deleted_at` = NULL, `created_at` = \'2018-10-30 09:33:55\', `updated_at` = \'2018-10-31 13:28:21\', \'2018-10-31 13:28:21\', \'Order:3\', \'\', 3, \'Updated\', \'2018-10-31 13:28:21\', \'\')' }

Any advice is greatly appreciated.


#2

JSON.stringify( modelInstance )