Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Answered by kyleconroy
sgtsquiggs asked this question in Q&A
Discussion options

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

You must be logged in to vote

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

Replies: 2 comments 1 reply

Comment options

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

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
1 reply
Comment options

nice, i tried double quotes and didn’t think to try single.

Answer selected by kyleconroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
bug Something isn't working 📚 postgresql
Converted from issue

This discussion was converted from issue #2832 on October 12, 2023 17:35.

AltStyle によって変換されたページ (->オリジナル) /