Repository navigation
Description
Feature Description
The Problem
In #1090, TypeORM supported orUpdate in its query builder to support upsert. But currently, it is only possible to perform an upsert when the single column violation happens. It seems not possible now to perform an upsert when a key constraint violation happens.
Assume that one has this DDL in the PostgreSQL database:
create table issue_synced_items
(
id uuid default uuid_generate_v4() not null
constraint issue_synced_items_pk
primary key,
created_at timestamp default (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text),
updated_at timestamp,
instance_id uuid,
left_id varchar(128),
right_id varchar(128),
constraint unique_pair
unique (left_id, right_id)
);it is possible to do an upsert on primary key violations by
await getConnection().createQueryBuilder(SyncedItems, "synced_items")
.insert()
.into(SyncedItems)
.values(item)
.orUpdate(['updated_at'],['id'])
.setParameter("t", new Date())
.execute()But if one wants to update on unique_pair constraint violation, a.k.a (left_id,right_id) already exists in the table, there is no way to achieve this by using orUpdate since the update will treat its' second parameter as a column.
The Solution
A Possible solution may be to add a third parameter for the orUpdate method so that it can properly handle the ON CONSTRAINT clause in the PostgreSQL-like database.
Considered Alternatives
Another possible solution for this problem is using
await getConnection().createQueryBuilder(SyncedItems, "synced_items")
.insert()
.into(SyncedItems)
.values(item)
.onConflict("ON CONSTRAINT unique_pair DO UPDATE SET updated_at = :t")
.setParameter("t", new Date())
.execute()If #8168 is solved, I believe this problem can be solved ( in some way ).
Additional Context
document for on conflict clause in PostgreSQL
Relevant Database Driver(s)
| DB Type | Relevant |
|---|---|
aurora-data-api |
no |
aurora-data-api-pg |
no |
better-sqlite3 |
no |
cockroachdb |
no |
cordova |
no |
expo |
no |
mongodb |
no |
mysql |
no |
nativescript |
no |
oracle |
no |
postgres |
yes |
react-native |
no |
sap |
no |
sqlite |
no |
sqlite-abstract |
no |
sqljs |
no |
sqlserver |
no |
Are you willing to resolve this issue by submitting a Pull Request?
- ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
Activity
Ginden commentedon Mar 8, 2022
You can use
onConflictmethod ofInsertQueryBuilderright now (though, it's deprecated).kristijanhusak commentedon Sep 2, 2022
Do you know if there will be some alternative for this once it gets removed?
sylvainar commentedon Sep 10, 2025
Adding to this thread, I have this usecase that I don't think I can do any other way than with an onConflict, I want to be able to do a merge operation between the previous version of the line and the new one (here adding new planIds in the list everytime I get a new one):
Cprakhar commentedon Jan 10, 2026
@CNLHC I think the requested feature has been successfully implemented.
gioboa commentedon Jan 10, 2026
#11908 confirms that it working fine now.