-
Notifications
You must be signed in to change notification settings - Fork 947
Description
Version
1.30.0
What happened?
Summary
sqlc generates Go code with variable name collisions when a function parameter has the same name as the variable used to capture the result of db.ExecContext().
Expected Behavior
sqlc should generate code that avoids variable name collisions, perhaps by using a different variable name for the ExecContext result:
func (q *Queries) UpdateResult(ctx context.Context, state string, result pqtype.NullRawMessage, id uuid.UUID) (int64, error) {
execResult, err := q.db.ExecContext(ctx, updateResult, state, result, id)
// ^^^^^^^^^^ Different variable name to avoid collision
if err != nil {
return 0, err
}
return execResult.RowsAffected()
}
Workaround
Rename the column in the database schema to avoid common Go variable names:
result
→ query_result
, output_result
, or similar
data
→ payload
, content
, etc.
error
→ error_message
, failure_reason
, etc.
Impact
This bug prevents compilation of generated code when column names match common Go variable names used in the generated functions. It requires manual intervention to either:
- Rename database columns (breaking change)
- Manually edit generated code (lost on regeneration)
- Use a different tool
Additional Context
The issue specifically occurs with :execrows queries where the generated code needs to capture the sql.Result from ExecContext(). The variable name for this result appears to be derived from the first occurrence of a column name that could be used as a variable name, leading to the collision.
Relevant log output
cannot use q.db.ExecContext(ctx, updateResult, state, result, id) (value of interface type sql.Result) as pqtype.NullRawMessage value in assignment
result.RowsAffected undefined (type pqtype.NullRawMessage has no field or method RowsAffected)
Database schema
CREATE TABLE example_table (
id UUID PRIMARY KEY,
state TEXT NOT NULL,
result JSONB
);
SQL queries
-- name: UpdateResult :execrows
UPDATE example_table
SET state = $1, result = $2
WHERE id = $3;
Configuration
version: "2"
sql:
- engine: "postgresql"
queries: "queries.sql"
schema: "schema.sql"
gen:
go:
package: "db"
out: "db"
Playground URL
https://play.sqlc.dev/p/90ffd5b2a8875bf6729e501132b6ba8d0f81e34be46464ffa8cb21d2104d1a07
What operating system are you using?
Linux
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go