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.

Upsert expects unique constraint for `OneToOne` columns which imply uniqueness already

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mostafa-hzcommented, Feb 8, 2022

Solution

If I change shop to shop.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 was shop.id.

I’m not sure if it is expected behavior. @joeflateau shouldn’t col.propertyPath replaced with col.propertyName or col.propertyAliasName?

1reaction
joeflateaucommented, Feb 9, 2022

@futpib I’ve found the metadata for the one-to-many’s unique indices. I have a fix in testing now

Read more comments on GitHub >

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

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