drift icon indicating copy to clipboard operation
drift copied to clipboard

InsertOnConflict update with Value.absent() values causes InvalidDataException when existing record exists.

Open RoarGronmo opened this issue 5 months ago • 1 comments

In this insertOnConflictUpdate scenario (partly code below) I try to update a record, where some bool values already exists, they should not be utpdated (therefore here omitted), and I mean that theese values get the Value.absent(), so they should not be touched whatsoever.

But the console says this:

[+12289 ms] municipalityNumber: 4651, showCompanyInfo:true
.
.
.
[ +152 ms] DartError: InvalidDataException: Sorry, MunicipalityInfos5027TableCompanion(municipalityNumber: Value(4651), firmId: Value(1), groupId: Value(0), name: Value(Stryn kommune), showCompanyInfo: Value.absent(), showGroupInfo: Value.absent(), showMunicipalityInfo: Value.absent(), showPlantInfo: Value.absent(), showDeviationInfo: Value.absent(), url: Value(www.stryn.kommune.no), regulationUrl: Value(), mail: Value(), phone: Value(XX XX XX XX), emergencyPhone: Value(), rowid: Value.absent()) cannot be used for that because: 
           • showCompanyInfo: This value was required, but isn't present
           • showGroupInfo: This value was required, but isn't present
           • showMunicipalityInfo: This value was required, but isn't present
           • showPlantInfo: This value was required, but isn't present
           • showDeviationInfo: This value was required, but isn't present

The first line above I checked if my record actually exists ( for the debugging case), and it does.

I cannot see any wrongdoing in the code below, (MunicipalityInfos5027Table) which gives the error...

Future<void> get5027MTankMunicipalityInfo({
  required String municipalityNumber
}) async {
  await Api5027Service().getMTankMunicipalitiesMunicipalityNumberInfo(
      municipalityNumber: municipalityNumber
  ).then((info)async{
    if(info!=null){
      globals.mTankDatabase.transaction(()async{
        if(info.municipalityInfo!=null){
          globals.mTankDatabase.municipalityInfos5027Table.insertOnConflictUpdate(
            MunicipalityInfos5027TableCompanion(
              municipalityNumber: Value(info.municipalityInfo?.municipalityNumber??""),
              name: Value(info.municipalityInfo?.name??""),
              firmId: Value(info.firmInfo?.id??0),
              groupId: Value(info.groupInfo?.id??0),
              url: Value(info.municipalityInfo?.url??""),
              regulationUrl: Value(info.municipalityInfo?.regulationUrl??""),
              mail: Value(info.municipalityInfo?.mail??""),
              phone: Value(info.municipalityInfo?.phone??""),
              emergencyPhone: Value(info.municipalityInfo?.emergencyPhone??""),
            )
          );
        }
        if(info.firmInfo!=null){
          globals.mTankDatabase.firmInfos5027Table.insertOnConflictUpdate(
            FirmInfos5027TableCompanion(
              firmId: Value(info.firmInfo?.id??0),
              name: Value(info.firmInfo?.name??""),
              url: Value(info.firmInfo?.url??""),
              phone: Value(info.firmInfo?.phone??""),
            )
          );
        }
        if(info.groupInfo!=null){
          globals.mTankDatabase.groupInfos5027Table.insertOnConflictUpdate(
            GroupInfos5027TableCompanion(
              groupId: Value(info.groupInfo?.id??0),
              name: Value(info.groupInfo?.name??""),
              url: Value(info.groupInfo?.url??""),
              regulationUrl: Value(info.groupInfo?.regulationUrl??""),
              mail: Value(info.groupInfo?.mail??""),
              phone: Value(info.groupInfo?.phone??""),
              emergencyPhone: Value(info.groupInfo?.emergencyPhone??""),
            )
          );
        }
      });


    }else{
      Message.error("No info found for municipality $municipalityNumber");
    }
  });
}

As I am not mentioning the booleans here they will be replaced by Value.absent(), as I see in the error message.

Or has i totally misunderstood the role of the Value.absent() in the insertOnConflictUpdate() scenarios...

Here are the definition of the record:

import 'package:drift/drift.dart';

@DataClassName("MunicipalityInfo5027")
class MunicipalityInfos5027Table extends Table{
  TextColumn get municipalityNumber => text()();
  IntColumn get firmId => integer()();
  IntColumn get groupId => integer()();
  TextColumn get name => text()();
  BoolColumn get showCompanyInfo => boolean()();
  BoolColumn get showGroupInfo => boolean()();
  BoolColumn get showMunicipalityInfo => boolean()();
  BoolColumn get showPlantInfo => boolean()();
  BoolColumn get showDeviationInfo => boolean()();
  TextColumn get url => text()();
  TextColumn get regulationUrl => text()();
  TextColumn get mail => text()();
  TextColumn get phone => text()();
  TextColumn get emergencyPhone => text()();

  @override
  Set<Column> get primaryKey => {municipalityNumber};

}

Flutter doctor in case:

C:\FlutterProjects\mTank git:[m_tank_dev_2025_06_19_v0_0_1_1]
flutter doctor -v
[√] Flutter (Channel stable, 3.32.4, on Microsoft Windows [Version 10.0.22631.5472], locale nb-NO) [875ms]
    • Flutter version 3.32.4 on channel stable at c:\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6fba2447e9 (9 days ago), 2025-06-12 19:03:56 -0700
    • Engine revision 8cd19e509d
    • Dart version 3.8.1
    • DevTools version 2.45.1

[√] Windows Version (11 Pro 64-bit, 23H2, 2009) [5,9s]

[√] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [6,4s]
    • Android SDK at C:\Users\roarg\AppData\Local\Android\Sdk
    • Platform android-36, build-tools 36.0.0
    • ANDROID_HOME = C:\Users\roarg\AppData\Local\Android\Sdk
    • ANDROID_SDK_ROOT = C:\Users\roarg\AppData\Local\Android\Sdk
    • Java binary at: c:\program files\java\jdk-21\bin\java
      This JDK is specified in your Flutter configuration.
      To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version Java(TM) SE Runtime Environment (build 21.0.7+8-LTS-245)
    • All Android licenses accepted.

[√] Chrome - develop for the web [266ms]
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.47) [263ms]
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.36107.64
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2024.3) [45ms]
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2024.3.1.15
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)

[√] Android Studio (version 2024.3.2) [42ms]
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2024.3.2.15
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)

[√] Android Studio (version 2025.1.1) [41ms]
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2025.1.1.12
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13391695-b895.109)

[√] Android Studio (version 2025.1.2) [40ms]
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2025.1.2.6
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13391695-b895.109)

[√] Connected device (3 available) [528ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22631.5472]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 137.0.7151.120
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 137.0.3296.83

[√] Network resources [1 038ms]
    • All expected network resources are available.

• No issues found!

And pubspec.yaml, if it helps...

name: m_tank
description: "minTank v2.0"
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.0.1+1

environment:
  sdk: ^3.8.0

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.8

  #drift:
  drift: ^2.27.0

  #drift_db_viewer:
  drift_db_viewer: ^2.0.0

  #swagger_dart_code_generator:
  swagger_dart_code_generator: ^3.0.0
  chopper: ^8.0.0
  json_annotation: ^4.8.0

  #google_maps_flutter:
  google_maps_flutter: ^2.12.3

  #package_info_plus:
  package_info_plus: ^8.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  #drift_dev:
  drift_dev: ^2.27.0

  #swagger_dart_code_generator needs:
  build_runner: ^2.3.3
  chopper_generator: ^8.0.0
  json_serializable: ^6.6.1


  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^6.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/to/resolution-aware-images

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/to/asset-from-package

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/to/font-from-package

RoarGronmo avatar Jun 22 '25 10:06 RoarGronmo

Intermediate remedy is like this:

          var existing = await globals.mTankDatabase.getMunicipalityInfo(municipalityNumber).getSingleOrNull();

          globals.mTankDatabase.municipalityInfos5027Table.insertOnConflictUpdate(
            MunicipalityInfos5027TableCompanion(
              municipalityNumber: Value(info.municipalityInfo?.municipalityNumber??""),
              name: Value(info.municipalityInfo?.name??""),
              firmId: Value(info.firmInfo?.id??0),
              groupId: Value(info.groupInfo?.id??0),
              url: Value(info.municipalityInfo?.url??""),
              regulationUrl: Value(info.municipalityInfo?.regulationUrl??""),
              mail: Value(info.municipalityInfo?.mail??""),
              phone: Value(info.municipalityInfo?.phone??""),
              emergencyPhone: Value(info.municipalityInfo?.emergencyPhone??""),
              showCompanyInfo: Value(existing?.showCompanyInfo??false),
              showGroupInfo: Value(existing?.showGroupInfo??false),
              showMunicipalityInfo: Value(existing?.showMunicipalityInfo??false),
              showPlantInfo: Value(existing?.showPlantInfo??false),
              showDeviationInfo: Value(existing?.showDeviationInfo??false),
            )
          );

So it will slow the process down a little bit...

RoarGronmo avatar Jun 22 '25 11:06 RoarGronmo