-
Notifications
You must be signed in to change notification settings - Fork 1k
Open
Labels
Description
Version
1.30.0
What happened?
When a parameter like $1 is used multiple times throughout a query, it only infers the type from the first time it appears in the query. However, any subsequent usage is not type checked at all. For example, $1 could be used the first time as TEXT and the second time as INT. This is a very serious type safety issue.
I replicated this issue in CreateAuthor in the playground, where the parameter $1 is repeated 3 times.
I believe that this happens mainly with the engine because the managed db will catch this issue.
Relevant log output
Database schema
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
age INT
);SQL queries
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio, age
) VALUES (
$1, $1, $1
)
RETURNING *;Generated code
const createAuthor = `-- name: CreateAuthor :one
INSERT INTO authors (
name, bio, age
) VALUES (
$1, $1, $1
)
RETURNING id, name, bio, age
`
func (q *Queries) CreateAuthor(ctx context.Context, name string) (Author, error) {
row := q.db.QueryRowContext(ctx, createAuthor, name)
var i Author
err := row.Scan(
&i.ID,
&i.Name,
&i.Bio,
&i.Age,
)
return i, err
}Configuration
Playground URL
https://play.sqlc.dev/p/7451d6ac945d7f193bffc1455c8dd016cbb8403003bfbc2722d5ba6d7fbfe4f1
What operating system are you using?
No response
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
Reactions are currently unavailable