Brendonovich / prisma-client-rust

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

*_many operations should take WhereInput, not UniqueWhereInput #338

Closed tingfeng-key closed 1 year ago

tingfeng-key commented 1 year ago

First of all, according to my investigation results, it can be guessed that it is a bug in the engine. I also tried to find the specific problem, but failed

model RoleMenu {
  id         Int        @id @default(autoincrement())
  role_id    Int
  menu_id    Int
  deleted_at DateTime?

  @@unique([role_id, menu_id])
}
let role_id_menu_ids = vec![role_menu::role_id_menu_id(2, 1),role_menu::role_id_menu_id(2, 2),role_menu::role_id_menu_id(2, 3), role_menu::role_id_menu_id(2, 4)];
client
        .role_menu()
        .delete_many(role_id_menu_ids)
        .exec()
        .await?

or

client
        .role_menu()
        .update_many(
            role_id_menu_ids,
            vec![role_menu::deleted_at::set(Some(now_time()))],
        )
        .exec()
        .await?

specific error:

Error executing query: P2009 - `deleteManyRoleMenu`: Field does not exist in enclosing type.

or 

Error executing query: P2009 - `updateManyRoleMenu`: Field does not exist in enclosing type.
apollo-sturdy commented 1 year ago

For those that are having this issue, it seems the bug exists for create_many but not for create so I solved by looping inside a ._transaction() call like this:

    // Save to db
    prisma_client
        ._transaction()
        .run(|client| async move {
            for apr in aprs {
                client
                    .token_apr()
                    .create(
                        apr.apr,
                        token::UniqueWhereParam::KeyEquals(apr.token_key),
                        vec![token_apr::apr_set::connect(apr_set::id::equals(apr_set.id))],
                    )
                    .exec()
                    .await?;
            }
            Ok::<(), anyhow::Error>(())
        })
        .await?;
Brendonovich commented 1 year ago

Thanks for reminding me of this issue - Took a look at the DMMF schema and it turns out that update_many and delete_many take WhereInput and not WhereUniqueInput, so using compound keys (which are only available in WhereUniqueInput is not actually valid. This should get fixed in 0.7.0 since the way types are being generated is changing.

Also @apollo-sturdy I noticed you're manually using UniqueWhereParam - I'd recommend you don't do that. token::key::equals(apr.token_key) should work and will shield you against breaking changes to UniqueWhereParam.