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

JsonNullableFilter type is wrong #297

Open
mirror-kt opened this issue Mar 26, 2023 · 9 comments
Open

JsonNullableFilter type is wrong #297

mirror-kt opened this issue Mar 26, 2023 · 9 comments
Labels
bug Something isn't working generator
Milestone

Comments

@mirror-kt
Copy link

mirror-kt commented Mar 26, 2023

I wrote a model using the Json? type in schema.prisma and did a cargo prisma generate.

The generated code looks like the following, which is a compile error because it cannot accept Option.

model some_model {
    id                     String               @id @unique @db.Uuid
    some_column            Json?
}
pub enum JsonNullableFilter {
    Equals(::prisma_client_rust::serde_json::Value),
    Path(Vec<String>),
    StringContains(String),
    StringStartsWith(String),
    StringEndsWith(String),
    ArrayContains(Option<::prisma_client_rust::serde_json::Value>),
    ArrayStartsWith(Option<::prisma_client_rust::serde_json::Value>),
    ArrayEndsWith(Option<::prisma_client_rust::serde_json::Value>),
    Lt(::prisma_client_rust::serde_json::Value),
    Lte(::prisma_client_rust::serde_json::Value),
    Gt(::prisma_client_rust::serde_json::Value),
    Gte(::prisma_client_rust::serde_json::Value),
    Not(::prisma_client_rust::serde_json::Value),
}
pub mod some_model {
    pub fn equals(value: Option<::prisma_client_rust::serde_json::Value>) -> WhereParam {
        WhereParam::SomeColumn(_prisma::read_filters::JsonNullableFilter::Equals(value))
    }
}
@Brendonovich Brendonovich added bug Something isn't working generator labels Mar 27, 2023
@Brendonovich
Copy link
Owner

This is an interesting one... Turns out Prisma's null handling is way more complex than I expected. Making Equals contain an Option should do the trick for now but I feel like more will need to be done eventually.

@Brendonovich
Copy link
Owner

🙃
image

@Brendonovich
Copy link
Owner

Ok, so Prisma Client JS deals with nulls by exposing an enum like this, since it only has one null type:

pub enum JsonNullValueFilter {
    DbNull,
    JsonNull,
    AnyNull,
}

But Rust has both Option::None and serde_json::Value::Null, which I feel would be more ergonomic - though it doesn't account for AnyNull, which allows checking for both at once.
I'm tossing up adding an is_null function or doing some trait stuff to allow passing both serde_json::Value and JsonNullValueFilter to equals. I'm not sure I want Option<serde_json::Value> since it's 2/3 of JsonNullValueFilter but not as clear what's going on.

@Brendonovich Brendonovich added this to the 0.7.0 milestone May 1, 2023
@Brendonovich
Copy link
Owner

I've half-fixed this on c235317 - JsonNullableFilter::Equals now takes Option<serde_json::Value>.
Still unsure about AnyNull, maybe is_any_null() is the best option.

@tgrgds
Copy link

tgrgds commented May 17, 2023

Temporary fix that has worked for me is to set a default value instead:

model some_model {
    id                     String               @id @unique @db.Uuid
    some_column            Json                 @default("[]")
}

Is this the only workaround until your fix is released?

@Brendonovich
Copy link
Owner

I guess so, I haven't explored any workarounds myself

@m1guelpf
Copy link
Sponsor

m1guelpf commented Jul 30, 2023

Hit this issue as well (and switched to main to keep going). Noticed that any nullable JSON fields are absent from the SetParams enum, making it impossible to set our update their values (this also happens for arrays of custom Prisma enums).

model Predictions {
  id String @id @default(uuid())
  input Json
  metrics Json?
  output Json   @default("{}")
  webhookFilter WebhookEvent[]
}
#[derive(Debug, Clone)]
pub enum SetParam {
  Id(super::_prisma::write_params::StringParam),
  Input(super::_prisma::write_params::JsonParam),
  Output(super::_prisma::write_params::JsonParam),
}

@matthewgapp
Copy link

I have this same issue. @m1guelpf by chance, what sha did you reference when you switched to main? Code gen via the CLI on the latest main seems to be broken.

@m1guelpf
Copy link
Sponsor

@matthewgapp rev bb1c2d3 works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working generator
Projects
None yet
Development

No branches or pull requests

5 participants