@@ -595,7 +595,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
595595
596596 this . _aliasMap [ alias ] = prop . type ;
597597 cond = QueryHelper . processWhere ( cond , this . entityName , this . metadata , this . platform ) ! ;
598- const aliasedName = `${ fromAlias } .${ prop . name } ` ;
598+ let aliasedName = `${ fromAlias } .${ prop . name } # ${ alias } ` ;
599599 path ??= `${ ( Object . values ( this . _joins ) . find ( j => j . alias === fromAlias ) ?. path ?? entityName ) } .${ prop . name } ` ;
600600
601601 if ( prop . reference === ReferenceType . ONE_TO_MANY ) {
@@ -606,6 +606,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
606606 if ( type !== 'pivotJoin' ) {
607607 const oldPivotAlias = this . getAliasForJoinPath ( path + '[pivot]' ) ;
608608 pivotAlias = oldPivotAlias ?? this . getNextAlias ( prop . pivotTable ) ;
609+ aliasedName = `${ fromAlias } .${ prop . name } #${ pivotAlias } ` ;
609610 }
610611
611612 const joins = this . helper . joinManyToManyReference ( prop , fromAlias , alias , pivotAlias , type , cond , path ) ;
@@ -632,8 +633,10 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
632633 return ret . push ( f ) ;
633634 }
634635
635- if ( this . _joins [ f ] && type === 'where' ) {
636- return ret . push ( ...this . helper . mapJoinColumns ( this . type , this . _joins [ f ] ) as string [ ] ) ;
636+ const join = Object . keys ( this . _joins ) . find ( k => f === k . substring ( 0 , k . indexOf ( '#' ) ) ) ! ;
637+
638+ if ( join && type === 'where' ) {
639+ return ret . push ( ...this . helper . mapJoinColumns ( this . type , this . _joins [ join ] ) as string [ ] ) ;
637640 }
638641
639642 ret . push ( this . helper . mapper ( f , this . type ) as string ) ;
@@ -648,7 +651,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
648651 }
649652
650653 Object . keys ( this . _populateMap ) . forEach ( f => {
651- if ( ! fields . includes ( f ) && type === 'where' ) {
654+ if ( ! fields . includes ( f . replace ( / # \w + $ / , '' ) ) && type === 'where' ) {
652655 ret . push ( ...this . helper . mapJoinColumns ( this . type , this . _joins [ f ] ) as string [ ] ) ;
653656 }
654657
@@ -758,17 +761,19 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
758761 this . _populate . forEach ( ( { field } ) => {
759762 const [ fromAlias , fromField ] = this . helper . splitField ( field ) ;
760763 const aliasedField = `${ fromAlias } .${ fromField } ` ;
764+ const join = Object . keys ( this . _joins ) . find ( k => `${ aliasedField } #${ this . _joins [ k ] . alias } ` === k ) ! ;
761765
762- if ( this . _joins [ aliasedField ] && this . helper . isOneToOneInverse ( field ) ) {
763- return this . _populateMap [ aliasedField ] = this . _joins [ aliasedField ] . alias ;
766+ if ( this . _joins [ join ] && this . helper . isOneToOneInverse ( fromField ) ) {
767+ return this . _populateMap [ join ] = this . _joins [ join ] . alias ;
764768 }
765769
766770 if ( this . metadata . find ( field ) ?. pivotTable ) { // pivot table entity
767771 this . autoJoinPivotTable ( field ) ;
768- } else if ( meta && this . helper . isOneToOneInverse ( field ) ) {
769- const prop = meta . properties [ field ] ;
770- this . _joins [ prop . name ] = this . helper . joinOneToReference ( prop , this . alias , this . getNextAlias ( prop . pivotTable ?? prop . type ) , 'leftJoin' ) ;
771- this . _populateMap [ field ] = this . _joins [ field ] . alias ;
772+ } else if ( meta && this . helper . isOneToOneInverse ( fromField ) ) {
773+ const prop = meta . properties [ fromField ] ;
774+ const alias = this . getNextAlias ( prop . pivotTable ?? prop . type ) ;
775+ this . _joins [ join ] = this . helper . joinOneToReference ( prop , this . alias , alias , 'leftJoin' ) ;
776+ this . _populateMap [ join ] = this . _joins [ join ] . alias ;
772777 }
773778 } ) ;
774779
0 commit comments