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
450 stars 30 forks source link

Error updating field with null #361

Closed dmitry-kotorov closed 3 months ago

dmitry-kotorov commented 4 months ago

Hello,

I am using orm: ^4.0.1

Model:

model feedback2 {
  guid                  String   @id @db.Char(36)
  user_guid             String   @db.Char(36)
  rating                Int      @db.UnsignedTinyInt
  message               String?  @db.VarChar(5000)
  application           Int      @db.UnsignedTinyInt
  application_version   String   @default("0") @db.VarChar(100)
  device_name           String?  @db.VarChar(100)
  language              String   @db.VarChar(10)
  loaded_to_jira        Boolean  @default(false)
  agreed_store_review Boolean  @default(false)
  created               DateTime @default(now()) @db.Timestamp(0)
  changed               DateTime @default(now()) @db.Timestamp(0)

  @@index([user_guid], map: "feedback2_user_guid_IDX")
}

While updating one record with message = null using query:

Query:

        await tx.feedback2.update(
          data: PrismaUnion.$1(Feedback2UpdateInput(
               rating: PrismaUnion.$1(feedback.rating),
            application: PrismaUnion.$1(feedback.application),
            language: PrismaUnion.$1(feedback.language),
            message: PrismaUnion.$2(
              PrismaUnion.$1(
                NullableStringFieldUpdateOperationsInput(
                  set: feedback.message != null
                      ? PrismaUnion.$1(feedback.message!)
                      : PrismaUnion.$2(PrismaNull()),
                ),
              ),
            ),
            applicationVersion: PrismaUnion.$1(feedback.applicationVersion),
            deviceName: feedback.deviceName != null
                ? PrismaUnion.$1(feedback.deviceName!)
                : PrismaUnion.$2(PrismaUnion.$1(
                    NullableStringFieldUpdateOperationsInput(
                        set: PrismaUnion.$2(PrismaNull())))),
            loadedToJira: PrismaUnion.$1(feedback.loadedToJira),
            agreedStoreReview: PrismaUnion.$1(feedback.agreedStoreReview),
            changed: PrismaUnion.$1(
                DateTime.fromMillisecondsSinceEpoch(feedback.changed * 1000)),
          )),
          where: Feedback2WhereUniqueInput(
            guid: feedback.guid,
          ),
        );

I am getting the following error:

Error:

 Exception: [{error: Error in query graph construction: QueryParserError(ValidationError { kind: Union, message: \"Unable to match input value to any allowed input type for the field. Parse errors: [Unable to match input value to any allowed input type for the field. Parse errors: [Invalid argument type. `message` should be of any of the following types: `String`, Some fields are missing: Expected exactly one field to be present, got 0., Invalid argument type. `message` should be of any of the following types: `Null`], Unable to match input value to any allowed input type for the field. Parse errors: [Invalid argument type. `message` should be of any of the following types: `String`, Some fields are missing: Expected exactly one field to be present, got 0., Invalid argument type. `message` should be of any of the following types: `Null`]]\", meta: Some(Object {\"errors\": Array [Object {\"kind\": String(\"Union\"), \"errors\": Array [Object {\"kind\": String(\"InvalidArgumentType\"), \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"argument\": Object {\"name\": String(\"message\"), \"typeNames\": Array [String(\"String\")]}, \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"inferredType\": String(\"Object\")}, Object {\"kind\": String(\"SomeFieldsMissing\"), \"inputType\": Object {\"kind\": String(\"object\"), \"name\": String(\"NullableStringFieldUpdateOperationsInput\"), \"fields\": Array [Object {\"name\": String(\"set\"), \"typeNames\": Array [String(\"String\"), String(\"Null\")], \"required\": Bool(false)}]}, \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"constraints\": Object {\"minFieldCount\": Number(1), \"maxFieldCount\": Number(1), \"requiredFields\": Null}}, Object {\"kind\": String(\"InvalidArgumentType\"), \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"argument\": Object {\"name\": String(\"message\"), \"typeNames\": Array [String(\"Null\")]}, \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"inferredType\": String(\"Object\")}]}, Object {\"kind\": String(\"Union\"), \"errors\": Array [Object {\"kind\": String(\"InvalidArgumentType\"), \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"argument\": Object {\"name\": String(\"message\"), \"typeNames\": Array [String(\"String\")]}, \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"inferredType\": String(\"Object\")}, Object {\"kind\": String(\"SomeFieldsMissing\"), \"inputType\": Object {\"kind\": String(\"object\"), \"name\": String(\"NullableStringFieldUpdateOperationsInput\"), \"fields\": Array [Object {\"name\": String(\"set\"), \"typeNames\": Array [String(\"String\"), String(\"Null\")], \"required\": Bool(false)}]}, \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"constraints\": Object {\"minFieldCount\": Number(1), \"maxFieldCount\": Number(1), \"requiredFields\": Null}}, Object {\"kind\": String(\"InvalidArgumentType\"), \"argumentPath\": Array [String(\"data\"), String(\"message\")], \"argument\": Object {\"name\": String(\"message\"), \"typeNames\": Array [String(\"Null\")]}, \"selectionPath\": Array [String(\"updateOnefeedback2\")], \"inferredType\": String(\"Object\")}]}]}) }), user_facing_error: {is_panic: false, message: Unable to match input value to any allowed input type for the field. Parse errors: [Unable to match input value to any allowed input type for the field. Parse errors: [Invalid argument type. `message` should be of any of the following types: `String`, Some fields are missing: Expected exactly one field to be present, got 0., Invalid argument type. `message` should be of any of the following types: `Null`], Unable to match input value to any allowed input type for the field. Parse errors: [Invalid argument type. `message` should be of any of the following types: `String`, Some fields are missing: Expected exactly one field to be present, got 0., Invalid argument type. `message` should be of any of the following types: `Null`]], meta: {kind: Union, errors: [{kind: Union, errors: [{kind: InvalidArgumentType, argumentPath: [data, message], argument: {name: message, typeNames: [String]}, selectionPath: [updateOnefeedback2], inferredType: Object}, {kind: SomeFieldsMissing, inputType: {kind: object, name: NullableStringFieldUpdateOperationsInput, fields: [{name: set, typeNames: [String, Null], required: false}]}, argumentPath: [data, message], selectionPath: [updateOnefeedback2], constraints: {minFieldCount: 1, maxFieldCount: 1, requiredFields: null}}, {kind: InvalidArgumentType, argumentPath: [data, message], argument: {name: message, typeNames: [Null]}, selectionPath: [updateOnefeedback2], inferredType: Object}]}, {kind: Union, errors: [{kind: InvalidArgumentType, argumentPath: [data, message], argument: {name: message, typeNames: [String]}, selectionPath: [updateOnefeedback2], inferredType: Object}, {kind: SomeFieldsMissing, inputType: {kind: object, name: NullableStringFieldUpdateOperationsInput, fields: [{name: set, typeNames: [String, Null], required: false}]}, argumentPath: [data, message], selectionPath: [updateOnefeedback2], constraints: {minFieldCount: 1, maxFieldCount: 1, requiredFields: null}}, {kind: InvalidArgumentType, argumentPath: [data, message], argument: {name: message, typeNames: [Null]}, selectionPath: [updateOnefeedback2], inferredType: Object}]}]}, error_code: P2009}}]","data":null,"level":"debug","host":null,"errorUrl":null,"method":null,"userIp":null,"forwardedFor":null,"env":null,"agent":null,"trace":"#0      BinaryEngine.request (package:orm/engines/binary.dart:193:44)\n<asynchronous suspension>\n#1      ActionClient.unserialized.<anonymous closure> (package:orm/src/runtime/action_client.dart:17:34)\n<asynchronous suspension>\n#2      Feedback2Delegate.update.<anonymous closure> (package:backend2/core/data/prisma/client.dart:7413:16)\n<asynchronous suspension>\n<asynchronous suspension>\n"}
kerimamansaryyev commented 3 months ago

Bumped to this today, temporary workaraound is writing the raw query as:

/// Needed because there is an error in Dart Prisma ORM which makes it
  /// impossible to set remind_at to null
  static Future<void> _mySQLQueryToUpdateRemindAt({
    required String reminderId,
    required DateTime? newDate,
    required PrismaClient intermediateClient,
  }) async {
    const query = 'UPDATE '
        ' `$_reminderRawTableName` '
        ' SET `$_reminderRawRemindAtColumnName` = ? '
        ' WHERE id = ?';
    await intermediateClient.$raw.execute(query, [
      newDate,
      reminderId,
    ]);
  }
medz commented 3 months ago

@dmitry-kotorov @kerimamansaryyev Update ORM to 4.2.1 version, I have verified that it has been fixed. If you still have this problem, please create a minimal repo to reproduce it and I will reopen the issue.