RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

Support custom field names without `@map` #964

Open D3f0 opened 4 months ago

D3f0 commented 4 months ago

Problem

Currently, using a schema like the one bellow will trigger the generator's default dmmf validation preventing using the name schema.

datasource client {
    provider = "sqlite"
    url = "file:db.sqlite"
}
generator client {
  provider             = "prisma-client-py"
  interface            = "asyncio"
  recursive_type_depth = 5
}

model Entity {
  id    Int     @id @default(autoincrement())
  email String  @unique
  schema  String?
}

When running poetry run prisma push

Suggested solution

Create fields with {reserver_name}_ for example, schema_ for the above case, and use Pyndantic aliases.

Alternatives

Create a custom generator.py, but run into problems:

Additional context

In my use case, the schema file is coming from a TypeScript project and I want to keep compatibility with it without changing the file.

RobertCraigie commented 2 months ago

If you weren't aware, this is achievable at the client level, without needing to perform db migrations with @map but this would mean you need to update your TypeScript project to use that field name as well.

I would not want automatically rename the field but supporting something like this would likely work quite well

model Entity {
  id    Int     @id @default(autoincrement())
  email String  @unique
  /// @Python(field_name: "schema_field")
  schema  String?
}