You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When using COALESCE in an UPDATE query, SQLC generates Go parameters with non-nullable types (e.g., int32, string) instead of nullable pointers (e.g., *int32, *string) or using pgtype. This prevents passing nil to skip updating specific fields, even though the query logic supports it.
Steps to Reproduce
Table Schema:
CREATETABLEIF NOT EXISTS lessons (
id BIGSERIALPRIMARY KEY,
index INTEGERNOT NULL, -- Mandatory field [[9]]
name TEXTNOT NULL, -- Mandatory field
description TEXTNOT NULL DEFAULT '',
is_online BOOLEANNOT NULL DEFAULT false,
dateTIMESTAMPNOT NULL,
subject_id BIGINTREFERENCES subjects (id),
class_id BIGINTREFERENCES classes (id),
teacher_id BIGINTREFERENCES users (id),
schedule_number INTEGERNOT NULL
);
Query:
-- name: UpdateLesson :execUPDATE lessons
SET
index = COALESCE($1, index),
name = COALESCE($2, name),
description = COALESCE($3, description),
is_online = COALESCE($4, is_online),
date= COALESCE($5, date),
subject_id = COALESCE($6, subject_id),
class_id = COALESCE($7, class_id),
teacher_id = COALESCE($8, teacher_id),
schedule_number = COALESCE($9, schedule_number)
WHERE id = $10;
Expected Behavior
Parameters for fields wrapped in COALESCE should be nullable pointers (e.g., *int32, *string) to allow nil values for skipping updates
Actual Behavior
SQLC generates non-nullable types based on the table schema’s NOT NULL constraints, ignoring the query’s COALESCE logic
Workaround
Manually redefine the generated struct with nullable types or modify table to use nullable types.
Relevant log output
Database schema
CREATETABLEIF NOT EXISTS lessons (
id BIGSERIALPRIMARY KEY,
index INTEGERNOT NULL, -- Mandatory field [[9]]
name TEXTNOT NULL, -- Mandatory field
description TEXTNOT NULL DEFAULT '',
is_online BOOLEANNOT NULL DEFAULT false,
dateTIMESTAMPNOT NULL,
subject_id BIGINTREFERENCES subjects (id),
class_id BIGINTREFERENCES classes (id),
teacher_id BIGINTREFERENCES users (id),
schedule_number INTEGERNOT NULL
);
SQL queries
-- name: UpdateLesson :execUPDATE lessons
SET
index = COALESCE($1, index),
name = COALESCE($2, name),
description = COALESCE($3, description),
is_online = COALESCE($4, is_online),
date= COALESCE($5, date),
subject_id = COALESCE($6, subject_id),
class_id = COALESCE($7, class_id),
teacher_id = COALESCE($8, teacher_id),
schedule_number = COALESCE($9, schedule_number)
WHERE id = $10;
Version
1.28.0
What happened?
Describe the bug
When using
COALESCE
in an UPDATE query, SQLC generates Go parameters with non-nullable types (e.g.,int32
,string
) instead of nullable pointers (e.g.,*int32
,*string
) or using pgtype. This prevents passingnil
to skip updating specific fields, even though the query logic supports it.Steps to Reproduce
Expected Behavior
Parameters for fields wrapped in COALESCE should be nullable pointers (e.g., *int32, *string) to allow nil values for skipping updates
Actual Behavior
SQLC generates non-nullable types based on the table schema’s NOT NULL constraints, ignoring the query’s COALESCE logic
Workaround
Manually redefine the generated struct with nullable types or modify table to use nullable types.
Relevant log output
Database schema
SQL queries
Configuration
Playground URL
https://play.sqlc.dev/p/d01115f99caeeadb87e2229132ebea97c8a8c36278e2339b136bbd5a30fcdfd2
What operating system are you using?
Linux
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
The text was updated successfully, but these errors were encountered: