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

Incorrect selectAll typing on CTE with same tablename #1147

Open
JustGreg opened this issue Sep 16, 2024 · 1 comment
Open

Incorrect selectAll typing on CTE with same tablename #1147

JustGreg opened this issue Sep 16, 2024 · 1 comment
Labels
bug Something isn't working duplicate This issue or pull request already exists typescript Related to Typescript

Comments

@JustGreg
Copy link

Hello, there seems to be a type error when the CTE has the same name as an existing table.

// result1 should have id and name from 'pet' but got type of Person
const result1 = await db
  .with("person", (db) =>
    db
      .selectFrom("pet")
      .where("is_favorite", "=", true)
      .select(["id", "name"]),
  )
  .selectFrom("person")
  .selectAll()
  .execute();

// result2 should only have id and first_name but got type of Person
const result2 = await db
  .with("person", (db) =>
    db
      .selectFrom("person")
      .where("age", ">", 60)
      .select(["id", "first_name"]),
  )
  .selectFrom("person")
  .selectAll()
  .execute();

selectFrom always select from the existing table but postgresql is selecting the CTE.

playground link

@koskimas
Copy link
Member

koskimas commented Sep 17, 2024

This is a similar issue to this. We probably merge the tables instead of replacing the existing table. At least in this case, we should replace the person table in the database interface with the new CTE.

But to make your query cleaner, I'd avoid using an existing name for your CTE.

@igalklebanov igalklebanov added bug Something isn't working duplicate This issue or pull request already exists typescript Related to Typescript labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists typescript Related to Typescript
Projects
None yet
Development

No branches or pull requests

3 participants