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.

Add unique key support for save()

See original GitHub issue

Issue type:

[ ] question [ ] bug report [x] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Hello.

I have entity with unique field. I am trying to save passing this unique key. After 2nd save call, I get “duplicate key”, beacuse typeorm is inserting instead of updating, beacuse typeorm does not check for unique key, but primary key.

Unique key should be considered in save, when primary key is not provided.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
IlyaSiroshcommented, Jan 21, 2022

Also wondering, any progress yet?

4reactions
brunoMauricecommented, Jan 2, 2020

The upsert is done by using columns that identify the data, the primary key. As I know a constraint “Unique key” have not for purpose to identify a row on your table. If it’s the case that mean your column have to be include on your primary key.

To apply an upsert like you expect using the fact that there is a conflict is not a common case and should be handle differently than using Save.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How add unique key to existing table (with non uniques rows)
You can do as yAnTar advised. ALTER TABLE TABLE_NAME ADD Id INT AUTO_INCREMENT PRIMARY KEY. OR. You can add a constraint
Read more >
Create Unique Constraints - SQL Server - Microsoft Learn
In the grid under General, select Type and choose Unique Key from the drop-down list box to the right of the property, and...
Read more >
How to make a column unique in SQL? - LearnSQL.com
First we write ALTER TABLE, then we list the name of the table (in our example: product ), and next we add the...
Read more >
Difference between Unique Indexes and Unique Constraints ...
This article explores the difference between Unique Indexes and Unique Constraints in SQL Server.
Read more >
Defining Unique Constraints in JPA - Baeldung
A composite unique key is a unique key made up of a combination of columns. To define a composite unique key, we can...
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