@@ -731,7 +731,7 @@ func buildChildUpdOpForSetNull(
731731 updateExprs := ctx .SemTable .GetUpdateExpressionsForFk (fk .String (updatedTable ))
732732 compExpr := nullSafeNotInComparison (ctx ,
733733 updatedTable ,
734- updateExprs , fk , updatedTable .GetTableName (), nonLiteralUpdateInfo , false /* appendQualifier */ )
734+ updateExprs , fk , updatedTable .GetTableName (), fk . Table . GetTableName (), nonLiteralUpdateInfo , false /* appendQualifier */ )
735735 if compExpr != nil {
736736 childWhereExpr = & sqlparser.AndExpr {
737737 Left : childWhereExpr ,
@@ -812,12 +812,20 @@ func createFKVerifyOp(
812812// and Child.c2 is not null and not ((Child.c1) <=> (Child.c2 + 1))
813813// limit 1
814814func createFkVerifyOpForParentFKForUpdate (ctx * plancontext.PlanningContext , updatedTable * vindexes.Table , updStmt * sqlparser.Update , pFK vindexes.ParentFKInfo ) Operator {
815- childTblExpr := updStmt .TableExprs [0 ].(* sqlparser.AliasedTableExpr )
815+ // Alias the foreign key's parent table name
816+ parentTblExpr := sqlparser .NewAliasedTableExpr (pFK .Table .GetTableName (), "parent" )
817+ parentTbl , err := parentTblExpr .TableName ()
818+ if err != nil {
819+ panic (err )
820+ }
821+
822+ // Alias the foreign key's child table name
823+ childTblExpr := sqlparser .NewAliasedTableExpr (updatedTable .GetTableName (), "child" )
816824 childTbl , err := childTblExpr .TableName ()
817825 if err != nil {
818826 panic (err )
819827 }
820- parentTbl := pFK . Table . GetTableName ()
828+
821829 var whereCond sqlparser.Expr
822830 var joinCond sqlparser.Expr
823831 var notEqualColNames sqlparser.ValTuple
@@ -894,7 +902,7 @@ func createFkVerifyOpForParentFKForUpdate(ctx *plancontext.PlanningContext, upda
894902 sqlparser .NewJoinTableExpr (
895903 childTblExpr ,
896904 sqlparser .LeftJoinType ,
897- sqlparser . NewAliasedTableExpr ( parentTbl , "" ) ,
905+ parentTblExpr ,
898906 sqlparser .NewJoinCondition (joinCond , nil )),
899907 },
900908 sqlparser .NewWhere (sqlparser .WhereClause , whereCond ),
@@ -931,12 +939,20 @@ func createFkVerifyOpForChildFKForUpdate(ctx *plancontext.PlanningContext, updat
931939 if ! ctx .VerifyAllFKs {
932940 panic (vterrors .VT12002 (updatedTable .String (), cFk .Table .String ()))
933941 }
934- parentTblExpr := updStmt .TableExprs [0 ].(* sqlparser.AliasedTableExpr )
942+
943+ parentTblExpr := sqlparser .NewAliasedTableExpr (updatedTable .GetTableName (), "parent" )
935944 parentTbl , err := parentTblExpr .TableName ()
936945 if err != nil {
937946 panic (err )
938947 }
939- childTbl := cFk .Table .GetTableName ()
948+
949+ // Alias the foreign key's child table name
950+ childTblExpr := sqlparser .NewAliasedTableExpr (cFk .Table .GetTableName (), "child" )
951+ childTbl , err := childTblExpr .TableName ()
952+ if err != nil {
953+ panic (err )
954+ }
955+
940956 var joinCond sqlparser.Expr
941957 for idx := range cFk .ParentColumns {
942958 joinExpr := & sqlparser.ComparisonExpr {
@@ -967,7 +983,7 @@ func createFkVerifyOpForChildFKForUpdate(ctx *plancontext.PlanningContext, updat
967983 // For example, if we are setting `update child cola = :v1 and colb = :v2`, then on the parent, the where condition would look something like this -
968984 // `:v1 IS NULL OR :v2 IS NULL OR (cola, colb) NOT IN ((:v1,:v2))`
969985 // So, if either of :v1 or :v2 is NULL, then the entire condition is true (which is the same as not having the condition when :v1 or :v2 is NULL).
970- compExpr := nullSafeNotInComparison (ctx , updatedTable , updStmt .Exprs , cFk , parentTbl , nil /* nonLiteralUpdateInfo */ , true /* appendQualifier */ )
986+ compExpr := nullSafeNotInComparison (ctx , updatedTable , updStmt .Exprs , cFk , parentTbl , childTbl , nil /* nonLiteralUpdateInfo */ , true /* appendQualifier */ )
971987 if compExpr != nil {
972988 whereCond = sqlparser .AndExpressions (whereCond , compExpr )
973989 }
@@ -978,7 +994,7 @@ func createFkVerifyOpForChildFKForUpdate(ctx *plancontext.PlanningContext, updat
978994 sqlparser .NewJoinTableExpr (
979995 parentTblExpr ,
980996 sqlparser .NormalJoinType ,
981- sqlparser . NewAliasedTableExpr ( childTbl , "" ) ,
997+ childTblExpr ,
982998 sqlparser .NewJoinCondition (joinCond , nil )),
983999 },
9841000 sqlparser .NewWhere (sqlparser .WhereClause , whereCond ),
@@ -992,7 +1008,7 @@ func createFkVerifyOpForChildFKForUpdate(ctx *plancontext.PlanningContext, updat
9921008// `:v1 IS NULL OR :v2 IS NULL OR (cola, colb) NOT IN ((:v1,:v2))`
9931009// So, if either of :v1 or :v2 is NULL, then the entire condition is true (which is the same as not having the condition when :v1 or :v2 is NULL)
9941010// This expression is used in cascading SET NULLs and in verifying whether an update should be restricted.
995- func nullSafeNotInComparison (ctx * plancontext.PlanningContext , updatedTable * vindexes.Table , updateExprs sqlparser.UpdateExprs , cFk vindexes.ChildFKInfo , parentTbl sqlparser.TableName , nonLiteralUpdateInfo []engine.NonLiteralUpdateInfo , appendQualifier bool ) sqlparser.Expr {
1011+ func nullSafeNotInComparison (ctx * plancontext.PlanningContext , updatedTable * vindexes.Table , updateExprs sqlparser.UpdateExprs , cFk vindexes.ChildFKInfo , parentTbl , childTbl sqlparser.TableName , nonLiteralUpdateInfo []engine.NonLiteralUpdateInfo , appendQualifier bool ) sqlparser.Expr {
9961012 var valTuple sqlparser.ValTuple
9971013 var updateValues sqlparser.ValTuple
9981014 for idx , updateExpr := range updateExprs {
@@ -1007,7 +1023,7 @@ func nullSafeNotInComparison(ctx *plancontext.PlanningContext, updatedTable *vin
10071023 }
10081024 updateValues = append (updateValues , childUpdateExpr )
10091025 if appendQualifier {
1010- valTuple = append (valTuple , sqlparser .NewColNameWithQualifier (cFk .ChildColumns [colIdx ].String (), cFk . Table . GetTableName () ))
1026+ valTuple = append (valTuple , sqlparser .NewColNameWithQualifier (cFk .ChildColumns [colIdx ].String (), childTbl ))
10111027 } else {
10121028 valTuple = append (valTuple , sqlparser .NewColName (cFk .ChildColumns [colIdx ].String ()))
10131029 }
0 commit comments