question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MYSQL on duplicate key update copy values from inserted records

See original GitHub issue

didn’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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
juanluispazcommented, May 23, 2022

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 and isValid you do not need CustomBooleanTypeAdapter because MySql transform booleans automatically to 0 or 1 (it depends on the version of MySQL)

0reactions
kir4ikcommented, May 23, 2022

Thank you very much. Everything is fine

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found