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: id inferred as string instead of number #371

Open
karlhorky opened this issue Jan 8, 2025 · 0 comments
Open

Regression: id inferred as string instead of number #371

karlhorky opened this issue Jan 8, 2025 · 0 comments

Comments

@karlhorky
Copy link
Collaborator

karlhorky commented Jan 8, 2025

Describe the bug

SafeQL 3.6.2 - 3.6.4 infer id as string instead of number - when CTE is used with coalesce() or joins:

// 💥 Query has incorrect type annotation.
//	Expected: { id: number; slug: string | null }[]
//	  Actual: { id: string; slug: string | null }[]
await sql<{ id: number; slug: string | null; }[]>`
  WITH
    x AS (SELECT * FROM curricula_appointments)
  SELECT
    x.id, -- Inferred incorrectly as string
    coalesce(appointments.slug, lectures.slug) AS slug
  FROM
    x
    INNER JOIN appointments ON x.appointment_id = appointments.id
    LEFT JOIN lectures ON appointments.lecture_id = lectures.id
`

Removing coalesce() but keeping the aliased slug field changes id type to string | null:

// 💥 Query has incorrect type annotation.
//	Expected: { id: number; slug: string | null }[]
//	  Actual: { id: string | null; slug: string | null }[]
await sql<{ id: number; slug: string | null }[]>`
  WITH
    x AS (
      SELECT
        *
      FROM
        curricula_appointments
    )
  SELECT
    x.id,
    lectures.slug AS slug
  FROM
    x
    INNER JOIN appointments ON x.appointment_id = appointments.id
    LEFT JOIN lectures ON appointments.lecture_id = lectures.id
`

Removing the slug field altogether returns id to number:

// ✅
await sql<{ id: number; }[]>`
  WITH
    x AS (SELECT * FROM curricula_appointments)
  SELECT
    x.id
  FROM
    x
    INNER JOIN appointments ON x.appointment_id = appointments.id
    LEFT JOIN lectures ON appointments.lecture_id = lectures.id
`

To Reproduce
Steps to reproduce the behavior:

See above

Expected behavior

id should be inferred as a number (id integer PRIMARY KEY)

Screenshots

--

Desktop (please complete the following information):

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

Additional context

Possibly originally introduced in this PR:

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

1 participant