Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport composite foreign keys #956
Comments
This comment has been minimized.
|
Unfortunately, this isn't going to happen in the near future. If we can add this backwards compatibly once generic associated types land, we will revisit it then. Right now though, I'm clearing out any issues that require breaking changes or new language features, as they're not actionable at this point in time. |
sgrif
closed this
Dec 16, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YetAnotherMinion commentedJun 18, 2017
•
edited
What do you want to do and how do you expect Diesel to support you with that?
I would like to use a composite foreign key on the child table when I am using composite primary keys on the primary table. I would be happy with the ability to implement the
BelongsTotrait for the child struct and the respectiveJoinTotrait for the parent struct while continuing to deriveInsertable,Queryable, andIdentifiablefor both child and parent.How do you think this can be added to Diesel?
Modify the signature ofBelongsTo::foreign_key(&self)from a type reference to just a type: change return type fromOption<&Self::ForeignKey>toOption<Self::ForeignKey>. Then update theBelongsTo!macro to setBelongsTo::ForeignKeyto&$foreign_key_tyinstead: line 151. Also change theBelongsTo!macro on line 154 to return theOption<Self::ForeignKey>instead ofOption<&$foreign_key_ty>.I have no idea. I tried above modification and I do not yet understand how to fix the GroupBy trait.
The end result would be the user can manually implement
BelongsTotrait for a composite foreign key like soFor real bonus points the derive macro could be changed to support specifying a composite foreign key so that composite foreign keys could be derived just like
Identifiableis right now.Possibly
ForeignKeyColumn: Columncould be replaced withForeignKeyExpression: Expression(because it looks like it is only used in one place and it only uses theeq_anyfunction. I don't understand the design intention ofForeignKeyColumntype, but perhaps the trait bound could be relaxed here? The presence of the extra trait typeForeignKeyColumnappears to be the primary difference from theIdentifiabletrait.A similar change to support composite primary keys was implemented in #510, and it looks like there will be a similar level of effort to support composite foreign keys.
What are possible alternatives?
Don't change anything and keep using separate structs for
InsertableandQueryable.Are there any disadvantages?
It is a public API change so a major version bump will be required.
Those using derive should not notice, but everyone who has a custom implementation of
BelongsTotrait will have to update their code. However the lifetimes of everything will stay the same, so looks like it would be a easy change for users.