-
Notifications
You must be signed in to change notification settings - Fork 962
proposal: add support for relation/edge schema #1949
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
Comments
Cc @tarrencev |
This works great for my use case! Maybe it makes most sense to start with the field.ID annotation and then work on the Through api? For now we could use the more verbose code |
Looks great. I like the |
This would be an amazing addition. Would be the last piece of the puzzle to be able to switch to Regarding the defaults in the through tables. Would it maybe be possible to detect if a table is referenced to as a "through" table, and then check if any non-default fields are missing, then panic/error out?
Nevermind the top suggestion, realized that it would no longer add an ID field, and the 2 custom ID fields would be defined by the user field types themselves. |
a8m := client.User.Create().SetName(..).SaveX(ctx)
// GroupUsersThrough has setters for fields except "group"
thr := client.Group.UsersThrough().
SetUser(a8m).
SetCreatedAt(time.Now)
hub := client.Group.Create().SetName(..).AddUsersThrough(thr).SaveX(ctx) |
Any particular reason this is added as annotation and not as part of the schema methods? |
This would solve the issue I’m running into now as well. The changes to API flow isn’t a big deal because now that logic can be customized for upserts. |
Any update on this? |
1 similar comment
Any update on this? |
While I started implementing, I started to think of a different approach. Defining a new schema type named // Tweet schema.
type Tweet struct {
ent.Schema
}
func (Tweet) Edges() []ent.Edge {
return []ent.Edge{
edge.To("users_liked", User.Type).
// Through allows defining M2M (join tables) explicitly using edge.Schemas.
// The first argument ("likes") allows interacting with the join table by its name.
Through("likes", TweetLike.Type),
}
}
// TweetLike schema (the join table).
// See the edge.Schema below.
type TweetLike struct {
edge.Schema
}
// Additional fields.
func (TweetLike) Fields() []ent.Field {
return []ent.Field{
field.Time("timestamp").
Default(time.Now),
}
}
func (TweetLike) Edges() []ent.Edge {
return []ent.Edge{
edge.To("tweet", Tweet.Type).
Unique(),
edge.To("user", User.Type).
Unique(),
}
} In the above example, there's no need/way to define the PK explicitly. It's always set to the foreign keys (as it is today). Composite PKs can be added, but I feel it's not really part of this feature. Usage API: Keep the generated code the same as it's today and provide extended API to allow query M2M edges (with Through) in 2 flavors: // Query without going through the join-table explicitly.
tweer.QueryUsersLiked().All(ctx)
// Query through.
tweer.QueryLikes().Where(tweetlike.TimestampGT(...)).QueryUsers().All(ctx) Thoughts? |
I have little experience with Entgo, but it sounds good to me! |
I like! 💯 |
Laravel uses the |
Each framework has its own specialized language, syntax and semantics. All I need as a user is a good introduction to the framework in the documentation. We could call it |
Thanks all for the feedback. I need to digest it a bit and come up with a final naming proposal.
|
Interestingly, however, you described it exactly this way, so that we understand it: (the join table)
|
❤️ love the example here and it would be a new milestone for ent to support this! One question, would there be any workaround (probably a less messy one) while we wait for this great addition to ent? |
Hey, |
The |
when i user Field.ID,error occurred. how can i use the options. |
why define unique? |
prefer this way. |
Hey all! The initial support was added, and it's in review atm. See #2560 and please feel free to share your thoughts/feedback before I merge it. |
Is the Will this feature be available for other relation types in addition to M2M ? Can mixins work for the Fields of the relation just like they already exist? The privacy policy on this is a really nice feature ;) |
Yes, the
Yes, it's mentioned in the docs.
All features of regular schemas are supported. e.g. Mixin, Hooks, and Privacy. |
Closed with #2560. Thank you all for the feedback. |
This is a feature that I have wanted to add for a long time to ent, and will be happy to get feedback, suggestion for improvements or just hear your thoughts before I create a proper PR for it.
The idea is instead of adding a completely new API for adding fields for edges (columns for join tables), configure their indexes, hooks, privacy, etc, or even support multi-column PKs, we'll add 2 new small changes for the ent/schema for supporting all those issues.
field.ID
AnnotationA new
field.ID
option will be added to theschema/field
package, and this will allow configuring multi-column PKs, but more than that, it will make it possible to configure relation/edge schema manually. For example:Indexes, hooks, privacy, and all other options will also be available for this type of schema, but in order to add a user to a group, the flow is a bit longer:
We can bypass this long flow by providing an additional config option for edges:
The
edge.Through
OptionThe
Through
option is supported by other frameworks and has already been proposed here before. The idea is to allow schemas that use the relation/edge-schema to CRUD directly their relations. However, I have an open question (atm) regarding the design of the generated code.The configuration looks as follows:
And the generated API is as follows:
An open question I have is on how do we set/update relation-schema fields in case they don't have default values or hooks configured on their ent/schema.
Thoughts?
The text was updated successfully, but these errors were encountered: