@@ -587,11 +587,42 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
587587 const combinedFilter = where ? conjunction ( this . dialect , [ where . where , policyFilter ] ) : policyFilter ;
588588 const selections = beforeUpdateAccessFields ?? QueryUtils . requireIdFields ( this . client . $schema , model ) ;
589589
590+ // Always qualify each column with its owning table. For delegate sub-models,
591+ // fields inherited from the base live in the base table and must be joined in.
592+ const baseModelsToJoin = new Set < string > ( ) ;
593+ const selectionNodes = selections . map ( ( f ) => {
594+ const fieldDef = QueryUtils . getField ( this . client . $schema , model , f ) ;
595+ const owningTable = fieldDef ?. originModel ?? model ;
596+ if ( fieldDef ?. originModel ) {
597+ baseModelsToJoin . add ( fieldDef . originModel ) ;
598+ }
599+ return SelectionNode . create ( ReferenceNode . create ( ColumnNode . create ( f ) , TableNode . create ( owningTable ) ) ) ;
600+ } ) ;
601+
602+ const idFields = QueryUtils . requireIdFields ( this . client . $schema , model ) ;
603+ const joins : JoinNode [ ] = Array . from ( baseModelsToJoin ) . map ( ( baseModel ) =>
604+ JoinNode . createWithOn (
605+ 'LeftJoin' ,
606+ TableNode . create ( baseModel ) ,
607+ conjunction (
608+ this . dialect ,
609+ idFields . map ( ( idField ) =>
610+ BinaryOperationNode . create (
611+ ReferenceNode . create ( ColumnNode . create ( idField ) , TableNode . create ( model ) ) ,
612+ OperatorNode . create ( '=' ) ,
613+ ReferenceNode . create ( ColumnNode . create ( idField ) , TableNode . create ( baseModel ) ) ,
614+ ) ,
615+ ) ,
616+ ) ,
617+ ) ,
618+ ) ;
619+
590620 const query : SelectQueryNode = {
591621 kind : 'SelectQueryNode' ,
592622 from : FromNode . create ( [ TableNode . create ( model ) ] ) ,
623+ joins : joins . length > 0 ? joins : undefined ,
593624 where : WhereNode . create ( combinedFilter ) ,
594- selections : selections . map ( ( f ) => SelectionNode . create ( ColumnNode . create ( f ) ) ) ,
625+ selections : selectionNodes ,
595626 } ;
596627 const result = await proceed ( query ) ;
597628 return { fields : beforeUpdateAccessFields , rows : result . rows } ;
0 commit comments