Skip to content

INSERT with SELECT causes *ast.ResTarget has nil name error #1646

Closed

Description

@ryan-berger

Version

1.13.0

What happened?

When trying to compile an INSERT/SELECT with sqlc.narg, I received the following error message:

query.sql:5:1: *ast.ResTarget has nil name

Relevant log output

SQLCDEBUG=dumpast=1 sqlc-dev generate
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d020)({
  Stmt: (*ast.CreateTableStmt)(0xc00024c960)({
   IfNotExists: (bool) false,
   Name: (*ast.TableName)(0xc000254a50)({
    Catalog: (string) "",
    Schema: (string) "",
    Name: (string) (len=1) "a"
   }),
   Cols: ([]*ast.ColumnDef) (len=1 cap=1) {
    (*ast.ColumnDef)(0xc000258780)({
     Colname: (string) (len=6) "temp_a",
     TypeName: (*ast.TypeName)(0xc000849490)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    })
   },
   ReferTable: (*ast.TableName)(<nil>),
   Comment: (string) "",
   Inherits: ([]*ast.TableName) <nil>
  }),
  StmtLocation: (int) 0,
  StmtLen: (int) 27
 })
}
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d040)({
  Stmt: (*ast.CreateTableStmt)(0xc00024c9c0)({
   IfNotExists: (bool) false,
   Name: (*ast.TableName)(0xc000254ab0)({
    Catalog: (string) "",
    Schema: (string) "",
    Name: (string) (len=1) "b"
   }),
   Cols: ([]*ast.ColumnDef) (len=2 cap=2) {
    (*ast.ColumnDef)(0xc000258820)({
     Colname: (string) (len=6) "temp_a",
     TypeName: (*ast.TypeName)(0xc000849500)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    }),
    (*ast.ColumnDef)(0xc0002588c0)({
     Colname: (string) (len=6) "temp_b",
     TypeName: (*ast.TypeName)(0xc000849570)({
      Catalog: (string) "",
      Schema: (string) "",
      Name: (string) (len=4) "text",
      Names: (*ast.List)(<nil>),
      TypeOid: (ast.Oid) 0,
      Setof: (bool) false,
      PctType: (bool) false,
      Typmods: (*ast.List)(<nil>),
      Typemod: (int32) 0,
      ArrayBounds: (*ast.List)(<nil>),
      Location: (int) 0
     }),
     IsNotNull: (bool) false,
     IsArray: (bool) false,
     Vals: (*ast.List)(<nil>),
     Length: (*int)(<nil>),
     Inhcount: (int) 0,
     IsLocal: (bool) false,
     IsFromType: (bool) false,
     IsFromParent: (bool) false,
     Storage: (uint8) 0,
     RawDefault: (ast.Node) <nil>,
     CookedDefault: (ast.Node) <nil>,
     Identity: (uint8) 0,
     CollClause: (*ast.CollateClause)(<nil>),
     CollOid: (ast.Oid) 0,
     Constraints: (*ast.List)(<nil>),
     Fdwoptions: (*ast.List)(<nil>),
     Location: (int) 0,
     Comment: (string) ""
    })
   },
   ReferTable: (*ast.TableName)(<nil>),
   Comment: (string) "",
   Inherits: ([]*ast.TableName) <nil>
  }),
  StmtLocation: (int) 28,
  StmtLen: (int) 41
 })
}
([]interface {}) (len=1 cap=1) {
 (*ast.RawStmt)(0xc00003d160)({
  Stmt: (*ast.InsertStmt)(0xc0003ba340)({
   Relation: (*ast.RangeVar)(0xc000254b40)({
    Catalogname: (*string)(<nil>),
    Schemaname: (*string)(<nil>),
    Relname: (*string)(0xc0005985e0)((len=1) "b"),
    Inh: (bool) true,
    Relpersistence: (uint8) 112,
    Alias: (*ast.Alias)(<nil>),
    Location: (int) 109
   }),
   Cols: (*ast.List)(0xc0005a1308)({
    Items: ([]ast.Node) (len=2 cap=2) {
     (*ast.ResTarget)(0xc000254b70)({
      Name: (*string)(0xc0005985f0)((len=6) "temp_a"),
      Indirection: (*ast.List)(0xc0005a1320)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.TODO)(0x22d91e0)({
      }),
      Location: (int) 111
     }),
     (*ast.ResTarget)(0xc000254ba0)({
      Name: (*string)(0xc000598610)((len=6) "temp_c"),
      Indirection: (*ast.List)(0xc0005a1338)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.TODO)(0x22d91e0)({
      }),
      Location: (int) 119
     })
    }
   }),
   SelectStmt: (*ast.SelectStmt)(0xc00025a630)({
    DistinctClause: (*ast.List)(0xc0005a1350)({
     Items: ([]ast.Node) <nil>
    }),
    IntoClause: (*ast.IntoClause)(<nil>),
    TargetList: (*ast.List)(0xc0005a1368)({
     Items: ([]ast.Node) (len=2 cap=2) {
      (*ast.ResTarget)(0xc000254bd0)({
       Name: (*string)(<nil>),
       Indirection: (*ast.List)(0xc0005a1380)({
        Items: ([]ast.Node) <nil>
       }),
       Val: (*ast.ColumnRef)(0xc00003d080)({
        Name: (string) "",
        Fields: (*ast.List)(0xc0005a1398)({
         Items: ([]ast.Node) (len=1 cap=1) {
          (*ast.String)(0xc000598630)({
           Str: (string) (len=6) "temp_a"
          })
         }
        }),
        Location: (int) 134
       }),
       Location: (int) 134
      }),
      (*ast.ResTarget)(0xc000254c90)({
       Name: (*string)(<nil>),
       Indirection: (*ast.List)(0xc0005a13b0)({
        Items: ([]ast.Node) <nil>
       }),
       Val: (*ast.CoalesceExpr)(0xc000254c60)({
        Xpr: (*ast.TODO)(0x22d91e0)({
        }),
        Coalescetype: (ast.Oid) 0,
        Coalescecollid: (ast.Oid) 0,
        Args: (*ast.List)(0xc0005a13c8)({
         Items: ([]ast.Node) (len=2 cap=2) {
          (*ast.FuncCall)(0xc000250ff0)({
           Func: (*ast.FuncName)(0xc000254c30)({
            Catalog: (string) "",
            Schema: (string) (len=4) "sqlc",
            Name: (string) (len=4) "narg"
           }),
           Funcname: (*ast.List)(0xc0005a13e0)({
            Items: ([]ast.Node) (len=2 cap=2) {
             (*ast.String)(0xc000598680)({
              Str: (string) (len=4) "sqlc"
             }),
             (*ast.String)(0xc0005986a0)({
              Str: (string) (len=4) "narg"
             })
            }
           }),
           Args: (*ast.List)(0xc0005a13f8)({
            Items: ([]ast.Node) (len=1 cap=1) {
             (*ast.A_Const)(0xc0005a1410)({
              Val: (*ast.String)(0xc0005986b0)({
               Str: (string) (len=3) "arg"
              }),
              Location: (int) 168
             })
            }
           }),
           AggOrder: (*ast.List)(0xc0005a1428)({
            Items: ([]ast.Node) <nil>
           }),
           AggFilter: (*ast.TODO)(0x22d91e0)({
           }),
           AggWithinGroup: (bool) false,
           AggStar: (bool) false,
           AggDistinct: (bool) false,
           FuncVariadic: (bool) false,
           Over: (*ast.WindowDef)(<nil>),
           Location: (int) 158
          }),
          (*ast.ColumnRef)(0xc00003d0e0)({
           Name: (string) "",
           Fields: (*ast.List)(0xc0005a1440)({
            Items: ([]ast.Node) (len=1 cap=1) {
             (*ast.String)(0xc0005986e0)({
              Str: (string) (len=6) "temp_b"
             })
            }
           }),
           Location: (int) 176
          })
         }
        }),
        Location: (int) 149
       }),
       Location: (int) 149
      })
     }
    }),
    FromClause: (*ast.List)(0xc0005a1458)({
     Items: ([]ast.Node) (len=1 cap=1) {
      (*ast.RangeVar)(0xc000254cc0)({
       Catalogname: (*string)(<nil>),
       Schemaname: (*string)(<nil>),
       Relname: (*string)(0xc000598720)((len=1) "a"),
       Inh: (bool) true,
       Relpersistence: (uint8) 112,
       Alias: (*ast.Alias)(<nil>),
       Location: (int) 189
      })
     }
    }),
    WhereClause: (*ast.TODO)(0x22d91e0)({
    }),
    GroupClause: (*ast.List)(0xc0005a1470)({
     Items: ([]ast.Node) <nil>
    }),
    HavingClause: (*ast.TODO)(0x22d91e0)({
    }),
    WindowClause: (*ast.List)(0xc0005a1488)({
     Items: ([]ast.Node) <nil>
    }),
    ValuesLists: (*ast.List)(0xc0005a14a0)({
     Items: ([]ast.Node) <nil>
    }),
    SortClause: (*ast.List)(0xc0005a14b8)({
     Items: ([]ast.Node) <nil>
    }),
    LimitOffset: (*ast.TODO)(0x22d91e0)({
    }),
    LimitCount: (*ast.TODO)(0x22d91e0)({
    }),
    LockingClause: (*ast.List)(0xc0005a14d0)({
     Items: ([]ast.Node) <nil>
    }),
    WithClause: (*ast.WithClause)(<nil>),
    Op: (ast.SetOperation) None,
    All: (bool) false,
    Larg: (*ast.SelectStmt)(<nil>),
    Rarg: (*ast.SelectStmt)(<nil>)
   }),
   OnConflictClause: (*ast.OnConflictClause)(<nil>),
   ReturningList: (*ast.List)(0xc0005a14e8)({
    Items: ([]ast.Node) (len=1 cap=1) {
     (*ast.ResTarget)(0xc000254cf0)({
      Name: (*string)(<nil>),
      Indirection: (*ast.List)(0xc0005a1500)({
       Items: ([]ast.Node) <nil>
      }),
      Val: (*ast.ColumnRef)(0xc00003d140)({
       Name: (string) "",
       Fields: (*ast.List)(0xc0005a1518)({
        Items: ([]ast.Node) (len=1 cap=1) {
         (*ast.A_Star)(0x22d91e0)({
         })
        }
       }),
       Location: (int) 201
      }),
      Location: (int) 201
     })
    }
   }),
   WithClause: (*ast.WithClause)(<nil>),
   Override: (ast.OverridingKind) 1
  }),
  StmtLocation: (int) 70,
  StmtLen: (int) 132
 })
}
# package batch
query.sql:5:1: *ast.ResTarget has nil name

Database schema

No response

SQL queries

CREATE TABLE a(temp_a TEXT);
CREATE TABLE b(temp_a TEXT, temp_b TEXT);

-- name: TestQuery :many
INSERT INTO b(temp_a, temp_c)
SELECT temp_a,
       COALESCE(sqlc.narg('arg'), temp_b)
FROM a
RETURNING *;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "postgresql",
      "name": "batch",
      "schema": "query.sql",
      "queries": "query.sql",
      "engine": "postgresql",
      "sql_package": "pgx/v4",
      "emit_json_tags": true,
      "emit_prepared_queries": true,
      "emit_interface": true
    }
  ]
}

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on May 27, 2022
  2. MS-24 commented on Mar 23, 2023

    @MS-24

    Facing similar issue with below insert statement.

    -- name: InsertOrders :exec
    	insert into Orders (id,name)
    	select id , CASE WHEN @name_do_update::BOOLEAN THEN @name ELSE s.name END 
    	from Orders s ;
    
  3. lavaniatwinkle commented on May 23, 2023

    @lavaniatwinkle

    DO we have a resolution for this yet? I am facing a similar issue.

  4. kyleconroy commented on Oct 24, 2023

    @kyleconroy
    Collaborator

    This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

    You can play around with the working example on the playground

  5. juannorris commented on Dec 31, 2024

    @juannorris

    Posting in case this is helpful for others coming to this issue.

    I was getting the same error when trying to do something like this:

    INSET INTO my_table (
      id,
      column_a
    )
    SELECT
      id,
      COALESCE(sqlc.narg('column_a'), column_a)
    FROM my_table
    WHERE id = @id;

    and while enabling managed DB did solve the issue, this alternative way of writing the query worked for me without enabling managed DB:

    INSET INTO my_table (
      id,
      column_a
    )
    SELECT
      my_table.id,
      COALESCE(sqlc.narg('column_a')::varchar, my_table.column_a)
    FROM my_table
    WHERE my_table.id = @id;
  6. OlegSchwann commented on Jul 16, 2025

    @OlegSchwann

    If you came to this page from a web search: just add the desired type.

    select sqlc.narg('description'); -->
    select sqlc.narg('description')::text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions