Skip to content

Argument name for sqlc.arg() can't be SQL keyword #2832

Description

@sgtsquiggs

Version

1.21.0

What happened?

sqlc errors out complaining about arguments

Relevant log output

# package models
queries.sql:1:1: Invalid argument to sqlc.arg()

Database schema

create table "user_data" (
	"id" varchar not null,
	"user" varchar not null,
	primary key ("id")
);

SQL queries

-- name: StageUserData: copyfrom
insert into "user_data" ("id", "user")
values (sqlc.arg(id), sqlc.arg(user));

Configuration

version: "2"
sql:
  - engine: "postgresql"
    schema: "schema.sql"
    queries: "queries.sql"
    gen:
      go:
        package: "models"
        sql_package: "pgx/v5"
        out: "models"

Playground URL

https://play.sqlc.dev/p/0dc89e4177151eca415a86676469f9cef991c373455641ddbacef05abab3f265

What operating system are you using?

macOS

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 Oct 11, 2023
  2. sgtsquiggs commented on Oct 11, 2023

    @sgtsquiggs
    Author

    I tried working around this by adding underscores to my parameter names, but the parameter names are used in the pgx CopyFrom statement instead of the proper column names.

    For example, if I use sqlc.arg(user_) it compiles fine, but the resulting code:

    func (q *Queries) StageUserData(ctx context.Context, arg []StageUserDataParams) (int64, error) {
    	return q.db.CopyFrom(ctx, []string{"user_data"}, []string{"id", "user_"}, &iteratorForStageUserData{rows: arg})
    }

    is incorrect - the insert statement defines the columns as id and user not id and user_.

    ☝️ this is a separate issue I will be filing here: #2833

  3. removed
    triageNew issues that hasn't been reviewed
    on Oct 12, 2023
  4. changed the title [-]Invalid argument to sqlc.arg() when the argument is `user`[/-] [+]Argument name for sqlc.arg() can't be SQL keyword[/+] on Oct 12, 2023
  5. kyleconroy commented on Oct 12, 2023

    @kyleconroy
    Collaborator

    user is a reserved keyword in PostgreSQL. To get around this limitation, put user in single quotes.

    -- name: StageUserData :copyfrom
    insert into "user_data" ("id", "user")
    values (sqlc.arg('id'), sqlc.arg('user'));

    https://play.sqlc.dev/p/9fa2777d8a0f6df949672259c122da0687e266745c84d2450bc291a9c701135f

  6. locked and limited conversation to collaborators on Oct 12, 2023
  7. converted this issue into a discussion #2837 on Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions