-
-
Notifications
You must be signed in to change notification settings - Fork 721
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
Add UNION support #207
Comments
Union Types / Polymorphic Associations are a common use case. It's one of the most requested Prisma features which was first requested 3 years ago and it still hasn't been impletemented. I'm currently building a web app using PostgreSQL where shareholders of a company could be either or both an Organization or an Individual Person and I'm having trouble deciding which approach to follow in order to avoid null columns in the
|
One solution would be a many:many table between shareholders and people/organizations, so that shareholders will always reference a row in that table. But then the many:many table will have null columns. |
OK, so if I understand correctly: with unions, you could select shareholders + inner join organizations, and then union select shareholders + inner join people. In that case, the inverstorId won't be a foreign key. Is that what you want to do? |
|
As far as schema modeling, could this be handled by adding an extra table? That is, you create an |
(Excuse the abuse of fields on an ER diagram, having example rows seems helpful to me) erDiagram
Organization 1--0+ Investor : is
Person 1--0+ Investor : is
Investor 1--0+ Investments : has
Investments 0+--1 Company: for
Organization {
name investor
MyOrg InvMyOrg
}
Person {
name investor
SomePerson InvSomePerson
}
Investor {
id x
InvMyOrg x
InvMyPerson x
}
Investments {
investor company
InvMyOrg MyCorp
InvSomePerson MyCorp
}
Company {
id x
MyCorp x
}
|
@luxaritas you will end up with a |
@cr101 The way I have this designed, it only has one column, its ID in the Investor table (which may as well be an autogenerated number or UUID). Instead of it having a foreign key out to organization and person, organization and person have a foreign key to it. |
The |
My assumption is that your end goal was just to tie a person/organization to the company they invest in. The investments table is the many:many join table (pretend you just had individuals investing in companies - in that case, each row would have an fk to the individual and to the company being invested in; in this case we add the investor table to aggregate both organization and individual investors). I guess in your original example, these aren't represented, and I was taking this a step further - the key point is that a person or organization points to a shareholder, not the other way around |
I was able to get a simple example of a Polymorphic Association working with the conditional operators.
The schema loos like this
And the result will look like this, Where in the response JSON if you are a doctor you will have a null as a patient value (and vice vera), But the DB will not contain nulls.
And this is what the sql looks like when i run
|
Available from drizzle-orm@0.29.0 |
@AndriiSherman @dankochetov and team/contributors on this ... honestly guys - congrats on completing this in less than a year compared to prisma who haven't even considered adding this yet... since 2020... 🥳 🍰 |
@AndriiSherman Sorry, where/how was this implemented? I don't see mention of it in the documentation nor in patch notes. EDIT: I think that this issue got erroneously merged with #1218 , which is the thing that patch 0.29.0 actually added support for. #1218 is talking about doing the UNION operation on sets. This issue is talking about supporting union types. The discussion is a little bit confusing because people are discussing different ways of modeling it in the DB, but I think that this post best describes what the proper solution to this problem is and what Drizzle could support: #207 (comment) We want Drizzle to automatically do the two conditional joins that @Hamsterzs implemented in that comment, and we want its Typescript typing to reflect that relationship. EDIT 2: Yeah, as #1051 mentions, this problem has not been solved and was erroneously merged with #1218 . Can someone with the permissions to merge issues merge this with #1051 so that people don't get confused by the statement that this problem has been solved? |
No description provided.
The text was updated successfully, but these errors were encountered: