Commit f448413
fix(orm): fix _count returning 0 for self-referential relations on delegate models
When buildCountJson builds a correlated subquery for _count, it was using
fieldModel as the subquery alias. For self-referential relations on delegate
models (where fieldModel === model), both sides of the correlated WHERE clause
resolved to the inner table, so no rows matched and count was always 0.
Fix by generating a unique subQueryAlias via tmpAlias() for the subquery,
matching the pattern already used by buildToOneRelationFilter and
buildRelationJoinFilter.
Fixes #2452
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent dbb3474 commit f448413
2 files changed
Lines changed: 55 additions & 4 deletions
File tree
- packages/orm/src/client/crud/dialects
- tests/regression/test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1292 | 1292 | | |
1293 | 1293 | | |
1294 | 1294 | | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
1295 | 1299 | | |
1296 | 1300 | | |
1297 | 1301 | | |
1298 | 1302 | | |
1299 | | - | |
| 1303 | + | |
1300 | 1304 | | |
1301 | 1305 | | |
1302 | | - | |
| 1306 | + | |
1303 | 1307 | | |
1304 | 1308 | | |
1305 | 1309 | | |
1306 | 1310 | | |
1307 | 1311 | | |
1308 | | - | |
| 1312 | + | |
1309 | 1313 | | |
1310 | 1314 | | |
1311 | 1315 | | |
1312 | 1316 | | |
1313 | | - | |
| 1317 | + | |
1314 | 1318 | | |
1315 | 1319 | | |
1316 | 1320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
0 commit comments