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

Filter out null values in SQL and resulting type #15113

Open
Akxe opened this issue Aug 31, 2022 · 1 comment
Open

Filter out null values in SQL and resulting type #15113

Akxe opened this issue Aug 31, 2022 · 1 comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: client types Types in Prisma Client

Comments

@Akxe
Copy link
Contributor

Akxe commented Aug 31, 2022

Problem

model Carrier {
  id   Int;
  lat? Float;
  lng? Float;
}
const carriers = await prisma.carrier.findMany({
  select: {
    id: true,
    lat: true,
    lng: true,
  },
  where: {
    lat: { not: { equals: null } },
    lng: { not: { equals: null } },
  },
});

// This now results in "number | null", but it very much should be "number"
carriers[0].lat

Suggested solution

I would prefer using a syntax like this:

const carriers = await prisma.carrier.findMany({
  select: {
    id: true,
    lat: 'notNull',
    lat: 'notNull',
  },
});

This is very descriptive that the lat and lng are only fetched if they are not null and it would remove a lot of unnecessary where clauses.

Alternatively, it could be done via the where clause, but the types would need to be fixed.

const carriers = await prisma.carrier.findMany({
  select: {
    id: true,
    lat: true,
    lng: true,
  },
  where: {
    lat: { not: { equals: null } },
    lng: { not: { equals: null } },
  },
});

Alternatives

Fixing type at runtime...

Additional context

Possible fix to this: #15074 - We could use the custom query to define a query that would fix the types, but it is a lot of manual work just in different a file...

@danstarns danstarns added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: client types Types in Prisma Client tech/typescript Issue for tech TypeScript. team/client Issue for team Client. kind/bug A reported bug. labels Aug 31, 2022
@Akxe
Copy link
Contributor Author

Akxe commented Sep 2, 2022

@danstarns, although I would love for this to be fixed, I do not think this qualifies as a bug but rather a feature.

@janpio janpio added kind/feature A request for a new feature. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels Sep 2, 2022
@janpio janpio changed the title Filter out null values in SQL and resulting type Filter out null values in SQL and resulting type Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: client types Types in Prisma Client
Projects
None yet
Development

No branches or pull requests

3 participants