Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
ON CONSTRAINT
  • Loading branch information
nikolayk812 committed Apr 1, 2026
commit c3d992202eb0468f534fd4256469dc695ea39fac
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ INSERT INTO servers(code, name) VALUES ($1, $2)
ON CONFLICT (code)
DO UPDATE SET name = EXCLUDED.name_typo;

-- name: UpsertServerMissingConflictTarget :exec
INSERT INTO servers(code, name) VALUES ($1, $2)
ON CONFLICT DO UPDATE SET name = EXCLUDED.name;

-- name: UpsertServerOnConstraintExcludedTypo :exec
INSERT INTO servers(code, name) VALUES ($1, $2)
ON CONFLICT ON CONSTRAINT servers_pkey DO UPDATE SET name = EXCLUDED.name_typo;

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# package querytest
query.sql:4:15: column "name_typo" of relation "servers" does not exist
query.sql:8:13: column "code_typo" of relation "servers" does not exist
query.sql:14:22: column "name_typo" of relation "servers" does not exist
query.sql:14:22: column "name_typo" of relation "EXCLUDED" does not exist
query.sql:17:1: ON CONFLICT DO UPDATE requires inference specification or constraint name
query.sql:22:61: column "name_typo" of relation "EXCLUDED" does not exist
27 changes: 23 additions & 4 deletions internal/sql/validate/insert_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)

const excludedTable = "EXCLUDED"

func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) error {
sel, ok := stmt.SelectStmt.(*ast.SelectStmt)
if !ok {
Expand Down Expand Up @@ -38,6 +40,7 @@ func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) er
Message: "INSERT has more expressions than target columns",
}
}

return onConflictClause(c, fqn, stmt)
}

Expand All @@ -47,7 +50,7 @@ func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) er
// - DO UPDATE SET col = ... assignment target columns exist
// - EXCLUDED.col references exist
func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt) error {
if n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
if fqn == nil || n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
return nil
}

Expand All @@ -62,13 +65,22 @@ func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt)
colNames[col.Name] = struct{}{}
}

// DO UPDATE requires a conflict target: ON CONFLICT (col) or ON CONFLICT ON CONSTRAINT name.
if n.OnConflictClause.Infer == nil {
return &sqlerr.Error{
Code: "42601",
Message: "ON CONFLICT DO UPDATE requires inference specification or constraint name",
}
}

// Validate ON CONFLICT (col, ...) conflict target columns.
if n.OnConflictClause.Infer != nil && n.OnConflictClause.Infer.IndexElems != nil {
if n.OnConflictClause.Infer.IndexElems != nil {
for _, item := range n.OnConflictClause.Infer.IndexElems.Items {
elem, ok := item.(*ast.IndexElem)
if !ok || elem.Name == nil {
continue
}

if _, exists := colNames[*elem.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name)
e.Location = n.OnConflictClause.Infer.Location
Expand All @@ -81,26 +93,30 @@ func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt)
if n.OnConflictClause.TargetList == nil {
return nil
}

for _, item := range n.OnConflictClause.TargetList.Items {
target, ok := item.(*ast.ResTarget)
if !ok || target.Name == nil {
continue
}

if _, exists := colNames[*target.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name)
e.Location = target.Location
return e
}

if ref, ok := target.Val.(*ast.ColumnRef); ok {
if excludedCol, ok := excludedColumnRef(ref); ok {
if _, exists := colNames[excludedCol]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, excludedCol)
e := sqlerr.ColumnNotFound(excludedTable, excludedCol)
e.Location = ref.Location
return e
}
}
}
}

return nil
}

Expand All @@ -110,13 +126,16 @@ func excludedColumnRef(ref *ast.ColumnRef) (string, bool) {
if ref.Fields == nil || len(ref.Fields.Items) != 2 {
return "", false
}

first, ok := ref.Fields.Items[0].(*ast.String)
if !ok || !strings.EqualFold(first.Str, "excluded") {
if !ok || !strings.EqualFold(first.Str, excludedTable) {
return "", false
}

second, ok := ref.Fields.Items[1].(*ast.String)
if !ok {
return "", false
}

return second.Str, true
}
Loading