Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.84k stars 108 forks source link

Unable to check if an optional relation is null #214

Closed jqphu closed 1 year ago

jqphu commented 1 year ago

Following from discord.

Given an optional relation

model User {

profile Profile?

}

It would be nice to filter on this field e.g. get all the users that don't have a profile.

Brendonovich commented 1 year ago

Not sure if this is a me problem or if the filters from Prisma don't allow a method to do this but I'll check it out

jqphu commented 1 year ago

Here's a snippet of something similar on typescript.

  const users = await prisma.user.findMany({
    where: {
      NOT: [
        {
          referredBy: null,
        }
      ],
      AND: [
        {
          referredOneMonthTrialExpiryDate: null,
        }
      ]
    }
  });

Finding all the users where the referredBy is not null, but the referredOneMonthTrialExpiryDate is null.

ReferredBy in this case is a relation, referredOneMonthTrialExpiryDate is just a simple DateTime.

Hope that's somewhat helpful.

Brendonovich commented 1 year ago

I've thought about it, and I think I'll need to custom-implement this operation myself. The example you provided is basically user::referred_by::equals(None) - but I don't provide an equals function for relations, and I don't plan on it since it only makes sense for null relations. Instead I'm thinking of adding an is_null filter which does the same thing. It'd look like user::referred_by::is_null().

EDIT: Done in 36372407d5080363b2e700390e3458e1c27c0b89

jqphu commented 1 year ago

That makes sense!

Thanks @Brendonovich :).