medz / prisma-dart

Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.
https://prisma.pub
BSD 3-Clause "New" or "Revised" License
436 stars 29 forks source link

Generated models have optional properties when defined as required in schema #336

Closed devklick closed 4 months ago

devklick commented 4 months ago

In my schema.prisma I have a model defined with what I understand to be a required field, since it's not using the optional modifier:

model Person {
  id   Int    @id @default(autoincrement())
  name String @db.VarChar(60)
}

I've run the generator as per the docs, with:

npx prisma generate

But the generated Person class has both the id and name properties as being optional:

class Person {
  const Person({
    this.id,
    this.name,
  });

  factory Person.fromJson(Map json) => Game(
        id: json['id'],
        name: json['name'],
      );

  final int? id;

  final String? name;
}

Is this the intended behaviour or have I missed something?

Using version 4.0.0-beta.3

medz commented 4 months ago

This is the expected behavior. All fields of model are optional fields. The main purpose is for me to adapt the select and include functions (for selecting only some fields or additional fields containing relationships)

devklick commented 4 months ago

@medz Thank you for clarifying.

Since this appears to be different behaviour to Prisma JS, it might be worth adding something in the model docs.

I'll close this issue as you've confirmed it's expected behaviour

medz commented 4 months ago

@devklick

Since this appears to be different behaviour to Prisma JS

Yes, since Prisma JS/TS relies on TS's type calculation, types can be constructed dynamically. Other languages do not have support for this feature. If you want to implement select/include, you can only use nullable types.