Upsert expects unique constraint for `OneToOne` columns which imply uniqueness already
See original GitHub issueI get this error:
An upsert requires conditions that have a unique constraint but none was found for conflict properties: original_response_id
For code like this:
import TypeORM from 'typeorm';
import { Response } from './response';
@TypeORM.Entity()
export class Archive {
@TypeORM.PrimaryGeneratedColumn('uuid')
id!: string;
@TypeORM.OneToOne(() => Response, response => response.id)
@TypeORM.JoinColumn()
originalResponse!: Response;
}
await this._archiveRepository.upsert(archive, [ 'original_response_id' ]);
I also tried changing 'original_response_id'
to 'original_response'
or 'originalResponse'
and it did not help.
This is the line where error is thrown: https://github.com/typeorm/typeorm/blob/26581d0264268045496b43f7a0f338eb9f2b70cc/src/entity-manager/EntityManager.ts#L509-L511
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Does @OneToOne imply uniqueness? - Stack Overflow
The foreign key column has the same type as the primary key of table B and there is a unique key constraint on...
Read more >UNIQUE Constraints in SQL - Simple Talk - Redgate Software
Here is an in-depth look at an underused constraint, UNIQUE, that can increase the performance of queries and protect data integrity.
Read more >Chapter 5. Basic O/R Mapping - NHibernate
Mapped classes must declare the primary key column of the database table. Most classes will also have a property holding the unique identifier...
Read more >Hibernate ORM 5.4.33.Final User Guide - Red Hat on GitHub
By default, JPA expects a database column having the same name with its ... Hibernate validates the uniqueness constraint when reloading the Phone...
Read more >Defining Constraints and Indexes
Creating/Dropping Foreign Key Constraints via ALTER; ON UPDATE and ON DELETE ... CheckConstraint is one where we expect the object to have a...
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 Free
Top 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
Solution
If I change
shop
toshop.id
it will work!https://github.com/typeorm/typeorm/blob/26581d0264268045496b43f7a0f338eb9f2b70cc/src/entity-manager/EntityManager.ts#L506
in the code above it checks for
col.propertyPath
which in my case wasshop.id
.I’m not sure if it is expected behavior. @joeflateau shouldn’t
col.propertyPath
replaced withcol.propertyName
orcol.propertyAliasName
?@futpib I’ve found the metadata for the one-to-many’s unique indices. I have a fix in testing now