sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
11.6k stars 746 forks source link

The interface type was generated incorrectly #3474

Open aimuz opened 5 days ago

aimuz commented 5 days ago

Version

1.26.0

What happened?

-- name: ListTemplates :many
SELECT *
FROM templates
WHERE (sqlc.arg(in_user_id) != TRUE OR user_id IN (sqlc.slice(user_ids)))
  AND (sqlc.arg(gt_updated_at) != TRUE OR updated_at > ?)
ORDER BY updated_at, id
LIMIT ?,?;

This is the parameter that generates the

type ListTemplatesParams struct {
    InUserID    interface{}  `db:"in_user_id"`
    UserIds     []int64      `db:"user_ids"`
    GtUpdatedAt interface{}  `db:"gt_updated_at"`
    UpdatedAt   sql.NullTime `db:"updated_at"`
    Offset      int32        `db:"offset"`
    Limit       int32        `db:"limit"`
}

I expect to generate the following structure

type ListTemplatesParams struct {
    InUserID    bool         `db:"in_user_id"`
    UserIds     []int64      `db:"user_ids"`
    GtUpdatedAt bool         `db:"gt_updated_at"`
    UpdatedAt   sql.NullTime `db:"updated_at"`
    Offset      int32        `db:"offset"`
    Limit       int32        `db:"limit"`
}

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

No response

What database engines are you using?

No response

What type of code are you generating?

No response

orisano commented 4 days ago

Which database engine are you using?

orisano commented 4 days ago

Having the URL to the playground would help expedite the investigation of the issue. https://play.sqlc.dev/

aimuz commented 4 days ago

https://play.sqlc.dev/p/5e94361639a0479b5ddc3624921392f5e6a36e9bc461ff7b7abb030ea339a12d

Sorry for not adding enough information, I shared a url

Diegiwg commented 4 days ago

Theoretically, you should be able to do the following:

-- name: ListTemplates :many
SELECT *
FROM templates
WHERE (sqlc.arg(in_user_id)::bool != TRUE OR user_id IN (sqlc.slice(user_ids)))
  AND (sqlc.arg(gt_updated_at)::bool != TRUE OR updated_at > ?)
ORDER BY updated_at, id
LIMIT ?,?;

With this, the generated data would be as expected, according to the documentation, but it ends up generating the following error:

sqlc generate failed.
# package 
query.sql:4:29: syntax error near "::bool != TRUE OR user_id IN (sqlc.slice(user_ids)))"
aimuz commented 4 days ago

Only postgresql supports this syntax.