sqlc
sqlc copied to clipboard
Column type overrides lost when selecting from a CTE
Version
1.15.0
What happened?
Rows returned by selecting from a CTE don't have the custom type overrides from the underlying columns. Parameters to the queries in the CTE do have the correct types.
In the example below, I expected to generate
func (q *Queries) FindOrCreateUser(ctx context.Context, email user.Email) (user.ID, error)
but I only got the correct type on the parameter while the result was the default type:
func (q *Queries) FindOrCreateUser(ctx context.Context, email user.Email) (int64, error)
Relevant log output
No response
Database schema
CREATE TABLE users (
id bigserial PRIMARY KEY,
email text NOT NULL CONSTRAINT unique_email UNIQUE
);
SQL queries
WITH inserted AS (
INSERT INTO users (email)
VALUES ($1)
ON CONFLICT DO NOTHING
RETURNING id
)
SELECT id
FROM inserted
UNION ALL
SELECT id
FROM users
WHERE email = $1;
Configuration
{
"version": "2",
"sql": [
{
"engine": "postgresql",
"schema": "query.sql",
"queries": "query.sql",
"gen": {
"go": {
"out": "db",
"overrides": [
{
"column": "users.id",
"go_type": "example.com/user.ID"
},
{
"column": "users.email",
"go_type": "example.com/user.Email"
}
]
}
}
}
]
}
Playground URL
https://play.sqlc.dev/p/61a01b4b0274dda093580349d104add205ecece9ac580dfc2fefc4d9a06b62fa
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
I ran into this today and found out that you can work around this by overriding the CTE table
WITH updated as ( ... ),
SELECT updated.* from updated
- column: "updated.body"
go_type: "path/to/type/dbtypes.SomeType"