Skip to content

Commit 103de1a

Browse files
authored
Fix relationship record retrieval
If a custom filter condition is supplied to any of the With{Type} methods the foreign key condition is not actually added to the query. While this doesn't break the retrieval it does pull a lot of unnecessary data into memory. For a type A that has a 1:N relationship to type B the queries look like these: ```sql SELECT __a.id, __a.c_id FROM a __a LIMIT 1 OFFSET 0; SELECT __b_Other.id, __b_Other.attribute, __b_Other.a_id FROM b __b_Other WHERE __b_Other.attribute = $1; -- current SELECT __b_Other.id, __b_Other.attribute, __b_Other.a_id FROM b __b_Other WHERE (__b_Other.attribute = $1 AND __b_Other.a_id IN ($2)); --fixed ``` ```golang type A struct { kallax.Model ID int64 `pk:"autoincr"` Other []B } type B struct { kallax.Model ID int64 `pk:"autoincr"` Attribute int64 } ```
1 parent e1a0ba6 commit 103de1a

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

batcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (r *batchQueryRunner) getRecordRelationships(ids []interface{}, rel Relatio
175175

176176
filter := In(fk, ids...)
177177
if rel.Filter != nil {
178-
And(rel.Filter, filter)
178+
rel.Filter = And(rel.Filter, filter)
179179
} else {
180180
rel.Filter = filter
181181
}

0 commit comments

Comments
 (0)