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

Unable to check if an optional relation is null #214

Closed
jqphu opened this issue Nov 25, 2022 · 4 comments · Fixed by #226
Closed

Unable to check if an optional relation is null #214

jqphu opened this issue Nov 25, 2022 · 4 comments · Fixed by #226

Comments

@jqphu
Copy link

jqphu commented Nov 25, 2022

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
Copy link
Owner

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
Copy link
Author

jqphu commented Nov 27, 2022

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
Copy link
Owner

Brendonovich commented Dec 17, 2022

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 3637240

@Brendonovich Brendonovich linked a pull request Dec 17, 2022 that will close this issue
@Brendonovich Brendonovich added this to the 0.6.4 milestone Dec 17, 2022
@jqphu
Copy link
Author

jqphu commented Dec 18, 2022

That makes sense!

Thanks @Brendonovich :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants