Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: inference of subquery aliased field name incorrect #362

Open
karlhorky opened this issue Jan 7, 2025 · 2 comments
Open

Regression: inference of subquery aliased field name incorrect #362

karlhorky opened this issue Jan 7, 2025 · 2 comments

Comments

@karlhorky
Copy link
Collaborator

karlhorky commented Jan 7, 2025

Describe the bug

SafeQL infers that the field in the subquery is id and not the aliased name rsvped:

// 💥 Query has incorrect type annotation.
//    Expected: { rsvped: boolean }[]
//      Actual: { id: number }[]
await sql<{ rsvped: boolean }[]>`
  SELECT
    x.*
  FROM
    (
      SELECT
        event_rsvps.id IS NOT NULL AS rsvped
      FROM
        event_rsvps
    ) x
`

Workaround

  1. Use explicit field names (x.rsvped instead of x.*)
  2. Switch boolean type to boolean | null (not accurate, but I think this is a separate SafeQL nullability inference error)
// ✅
await sql<{ rsvped: boolean | null }[]>`
  SELECT
    x.rsvped
  FROM
    (
      SELECT
        event_rsvps.id IS NOT NULL AS rsvped
      FROM
        event_rsvps
    ) x
`

To Reproduce

See above

Expected behavior

Inference of the aliased name rsvped in the subquery

Screenshots

--

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.2 (24C101)
  • PostgreSQL version 14.13
  • Version 3.6.2

Additional context

Originally introduced in this PR:

@Newbie012
Copy link
Collaborator

fixed in 3.6.3

@karlhorky
Copy link
Collaborator Author

karlhorky commented Jan 8, 2025

Hmm, still seeing this with @ts-safeql/[email protected] 🤔

Screenshot 2025-01-08 at 23 14 39

// 💥 Query has incorrect type annotation.
//    Expected: { rsvped: boolean }[]
//      Actual: { id: number }[]
await sql<{ rsvped: boolean }[]>`
  SELECT
    x.*
  FROM
    (
      SELECT
        event_rsvps.id IS NOT NULL AS rsvped
      FROM
        event_rsvps
    ) x
`

Here's the minimal table definition, in case that works for reproducing for you:

CREATE TABLE event_rsvps (
  id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY
);

@karlhorky karlhorky reopened this Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants