WIP: association-preloading #2241
No reviewers
Labels
No Label
backport/done
backport/v1
blocked
db
oracle
db
sqlserver
duplicate
feature
cache
frontport/done
frontport/main
invalid
kind
breaking
kind
bug
kind
build
kind
dependencies
kind
docs
kind
driver
kind
enhancement
kind
feature
kind
performance
kind
proposal
kind
question
kind
refactor
kind
testing
need
feedback
need
test
proposal:accepted
RaspBerry Pi
regression
skip-changelog
upstream
wip
wontfix
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: xorm/xorm#2241
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "sogari/xorm:association-preloading"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR is an attempt to implement the association preload feature. There are some restrictions at the moment (but maybe we can address them in a later version):
many_to_many
associations. This means that an FK forbelongs_to
must be in the owning (aka, parent or referencing) model, while an FK forhas_one
orhas_many
must be in the referenced model.many_to_many
associations cannot be declared as a model in the code. Its struct type and table schema are generated at runtime with two fields.One additional note: intermediate associations that have no selected columns and were not filled with any nested association are considered "empty" (except for the PK and FK fields) and are removed from the result by default. To get around this behavior, one can add the PK column to the list of selected columns of the intermediate preload (since this column will be fetched anyway).
Solves #2240
@ -85,3 +85,3 @@
// ParseWithCache parse a struct with cache
func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) {
t := v.Type()
t := reflect.Indirect(v).Type()
Without this, the same struct type was being parsed twice.
@ -144,0 +152,4 @@
return err
}
// we need the columns required for the preloads
if !session.statement.ColumnMap.IsEmpty() {
If it's empty, could we generate all the columns from the struct?
Hi @lunny, I’m sorry for the late response. I was involved in a motorcycle accident recently, and have been off for a few days.
About the
ColumnMap
being empty, if I’m not mistaken, xorm’s behavior in this case is to select all columns from the struct. In that case, the required columns for preloading will already be included and we don’t need to worry about it. Does this make sense?Could we ignore
Cols
of theengine.Proload.Cols
which means all the columns of the table?71389d298f
toc763b72c65
I'm not sure if I understood your question correctly. By "ignore", do you mean the same functionality as the
engine.Omit
function?No, I mean whether we can use like below.
Thanks for the clarification. I see what you mean now.
In the way I implemented it, a preload must have a
Cols
clause to select at least one column. Otherwise, the default behavior is to omit all columns, in order to avoid polluting intermediate preloads.I’ll see if I can change it to allow not specifying any column, and internally selecting all columns for a leaf preload.
c763b72c65
to89a266addf
Please resolve the conflicts.
Checkout
From your project repository, check out a new branch and test the changes.