Skip to content
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

Support compound unique in upsert #3656

Closed
Nisgrak opened this issue Oct 25, 2022 · 3 comments
Closed

Support compound unique in upsert #3656

Nisgrak opened this issue Oct 25, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Nisgrak
Copy link

Nisgrak commented Oct 25, 2022

Describe the bug
The new upsert() fails if you try to upsert with a compound index

Stack trace

Error: Unique property value required for upsert, provide one of: id

To Reproduce
Steps to reproduce the behavior:
https://stackblitz.com/edit/mikro-orm-esm-repro-y96acp?file=basic.test.ts

Expected behavior
Generate an insert on conflict 'my_autogenerate_compound_index'

@B4nan
Copy link
Member

B4nan commented Oct 25, 2022

Yes, this is not supported.

Funny how an explicit validation about something not being supported yields bug reports :D

@B4nan B4nan added the enhancement New feature or request label Oct 25, 2022
@Nisgrak
Copy link
Author

Nisgrak commented Oct 25, 2022

Yeah, maybe "bug" wasn't the aproppied word...

Anyway I see that Postgre and SQLite allow to specify what index have to conflict, maybe we can specify in the upsert()?

orm.em.upsert(User, { ... }, { index: "my_compound_index" } 

MariaDB doesn't seem to allow that.

@B4nan
Copy link
Member

B4nan commented Oct 25, 2022

This is not about indexes, the problem is comparing complex PKs, that is the reason why this is disallowed, as you can't simply compare things like a.id === b.id, neither you can build a map of pk <-> entity easily.

TBH I was just lazy implementing this, the em.upsert turned to be very complex already even without that...

If you wanna take a look, its this part of codebase: https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/EntityManager.ts#L523

This would have to additionally check composite unique constraints - those live on the entity metadata (meta.uniques), not on property level. I guess it won't be that hard to support, it was just too much to do in a single run.

@B4nan B4nan self-assigned this Nov 3, 2022
@B4nan B4nan closed this as completed in 3cf79d6 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants