PreloadWhere Documentation/Examples #172
-
I am trying to figure out how I can use the generated code to filter a select based on a value in a related to-one table. While digging through the repository I found the Could you show/add some documentation/examples on how to use it with generated |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
The best way to do this at the moment is to do a fresh join and then filter as you would: videos, err := models.Videos.Query(ctx, db,
models.PreloadVideoUser(),
models.SelectJoins(ctx).Videos.InnerJoin.User,
models.SelectWhere.Users.ID.GT(200),
).One() The generated query will has 2 joins, but the DB is smart enough to do the work just once. You can verify this using Why can't
|
Beta Was this translation helpful? Give feedback.
-
Ah sorry, should have opened a discussion instead of an issue. I played around with I tried to put what I want to achieve in an example query:
|
Beta Was this translation helpful? Give feedback.
The best way to do this at the moment is to do a fresh join and then filter as you would:
The generated query will has 2 joins, but the DB is smart enough to do the work just once. You can verify this using
EXPLAIN ANALYZE
Why can't
PreloadX
takeWhereMods
?The reason is that to preload, a random suffix is added to the name of the table. This isolates it from the rest of the query and makes it extremely unlikely that it conflicts with any other part of the query.
So, if you wanted to filter using the same join that was …