sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

sqlc completely skips over arguments in complex queries with no error messages

Open maddsua opened this issue 1 year ago • 2 comments

Version

1.27.0

What happened?

The generated output lacks any query parameters which yields it completely useless. All the sqlc.arg() are ignored with no errors raised. When switched annotation to batchexec the following error is encountered: :batch* commands require parameters

Relevant log output

No response

Database schema

CREATE TABLE usage (
  id   BIGSERIAL PRIMARY KEY,
  created_at timestampz not null,
  user_id bigint not null,
  delta  bigint
);

SQL queries

-- name: UpdateUsage :exec
merge into usage using (
	select usage.id
	from (select 1) as dummy
		left join usage
		on user_id = sqlc.arg(user_id)
			and created_at >= now() - interval '1 hour'
	order by id desc
	limit 1
) ref on usage.id = ref.id
when matched then
	update
	set delta = delta + sqlc.arg(delta)
when not matched then
	insert (user_id, delta)
	values (sqlc.arg(user_id), sqlc.arg(delta));

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "postgresql",
    "gen": {
      "go": {
        "out": "db"
      }
    }
  }]
}

Playground URL

https://play.sqlc.dev/p/b0b005a26fe2667afe52dfe4d229d18d4cde8e426ac83cbf4786ec050749875e

What operating system are you using?

Windows

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

maddsua avatar Oct 21 '24 10:10 maddsua

In my case I was using $1 instead of ? for MySQL. I changed to ? for arguments in case of MySQL and problem solved.

abdul-saqib avatar May 02 '25 10:05 abdul-saqib

I am seeing the same issue in postgres, also with a similar merge into query.

rpolyano avatar May 05 '25 10:05 rpolyano

Same issue when doing an update with a join in mysql https://play.sqlc.dev/p/eb804f98bf15502a525791aa81658fb2a48e42cdb4dade405e452d1f0ce18f33

meblum avatar Aug 19 '25 19:08 meblum

same as original, had batchexec insert query with on confilct clause - worked switched to merge into query and now i have :batch* commands require parameters

aimestereo avatar Aug 21 '25 16:08 aimestereo

i see, sqlc still doesn't have support for MERGE INTO: https://github.com/sqlc-dev/sqlc/issues/2272

aimestereo avatar Aug 25 '25 13:08 aimestereo

i see, sqlc still doesn't have support for MERGE INTO: #2272

Solid, but this behavior can also happen in other queries. Don't have any other rep. examples atm.

maddsua avatar Aug 26 '25 15:08 maddsua