MYSQL on duplicate key update copy values from inserted records
See original GitHub issuedidn’t find a way to use mysql VALUES
on a duplicate insert.
You have to write something like this to make it work.
this.connection
.insertInto(productTable)
.values(products)
.customizeQuery({
afterQuery: this.connection
.rawFragment`on duplicate key update product.amazonSalesRank = {VALUES(product.amazonSalesRank), product.brandId = VALUES(product.brandId), product.categoryId = VALUES(product.categoryId), product.isDeleted = VALUES(product.isDeleted), product.isValid = VALUES(product.isValid), product.itemsCount = VALUES(product.itemsCount), product.msrp = VALUES(product.msrp), product.name = VALUES(product.name), product.nrsSales = VALUES(product.nrsSales), product.reviewStatus = VALUES(product.reviewStatus), product.size = VALUES(product.size)}`
});
if use
.onConflictDoUpdateSet({
isDeleted: this.connection.fragmentWithType('boolean', 'required').sql`VALUES(${productTable.isDeleted})`
})
then the mysql will throw an error on such an expression in on duplicate key update
insert into product (...) values(...) on duplicate key update isDeleted = case when VALUES(isDeleted = 1) then 1 else 0 end
I need it to be so, then mysql will understand
insert into product (...) values(...) on duplicate key update product.isDeleted = VALUES(product.isDeleted)
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
13.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE...
Read more >On Duplicate Key Update same as insert - Stack Overflow
INSERT ON DUPLICATE KEY UPDATE leaves unmentioned columns unchanged on UPDATE but gives them default values on INSERT. With REPLACE INTO, ...
Read more >MySQL INSERT ON DUPLICATE KEY UPDATE
The INSERT ON DUPLICATE KEY UPDATE is a MySQL's extension to the SQL standard's INSERT statement. When you insert a new row into...
Read more >INSERT ON DUPLICATE KEY UPDATE - MariaDB
INSERT ... ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary...
Read more >INSERT ON DUPLICATE KEY UPDATE in MySQL
When the ON DUPLICATE KEY UPDATE option is defined in the INSERT statement, the existing rows are updated with the new values instead....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Release ts-sql-query 1.28.0 with support to reference current value and value to insert in an insert on conflict do update.
Fro more details see the documentation
Please, test it and give me feedback if that resolve your requirement.
Note: I believe in the columns
isDeleted
andisValid
you do not needCustomBooleanTypeAdapter
because MySql transform booleans automatically to 0 or 1 (it depends on the version of MySQL)Thank you very much. Everything is fine