New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: upsert should find unique index created by one-to-one relation #8618
fix: upsert should find unique index created by one-to-one relation #8618
Conversation
Thanks for the fix! Quick question: do we really need a runtime check for column uniqueness? Usually we delegate everything the db, and if there is something that db cannot handle - it throws an error. |
@pleerock I think it's important. It's really there to protect MySQL/MariaDB users since those DB engines don't let you specify the conflict columns. If you "upsert" and there's no unique constraint for the columns you specify as your conflict columns then Postgres will throw an error but MySQL will just insert it and move on. |
if that's natural MySQL behaviour, and users use MySQL's UPSERT with it's natural behaviour (orm is just a wrapper around what rdbms provides) then why do we need to prevent users from doing what they wanna do? |
my philosophy is that I like to catch footguns like this where the abstraction of "upsert" could cause some unexpected behavior due to db engine differences. your call though. I can remove the check altogether if you prefer that |
The problem is that we definitely cannot catch all the errors - and when we handle some errors one way and other errors another way - it brings a confusion. Also, there are users who use ORM with exist database without entities synchronization. Such users don't define entity indices and uniques. They simply define entities and execute queries against exist database. Current approach fail for them.
Yes, I would prefer these checks to be handles by database if it's possible. If you don't have a time or wish to do that - no worries, I'll merge this PR for now and someday we might refactor this code. |
thank you a lot |
For anyone that wants typing, I did this: const conflictPaths: (keyof MyEntity)[] = ['columnA', 'columnB']
const opts: UpsertOptions<MyEntity> = { conflictPaths };
myEntityRepo.upsert({ ... }, opts) |
Description of change
Fixes #8587
One-to-one relations create a unique index, that index should be suitable for an upsert conflict target.
Pull-Request Checklist
master
branchnpm run lint
passes with this changenpm run test
passes with this changeFixes #0000