Jaguar-dart / jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
https://jaguar-dart.github.io
BSD 3-Clause "New" or "Revised" License
217 stars 54 forks source link

Error when cascading upsert #173

Open outofculture opened 4 years ago

outofculture commented 4 years ago

I have a model with a many-to-many relationship:

class Session {
  @ManyToMany(StudentSessionBean, StudentBean)
  List<Student> students;
}

When I call session.id = await sessionBean.upsert(session, cascade: true) on a new session, I get the following error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'id' was called on null.
Receiver: null
Tried calling: id
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      _StudentSessionBean.attach (package:tablet_app/models/student_session.jorm.dart:473:25)
#2      _SessionBean.upsert (package:tablet_app/models/session.jorm.dart:202:36)

And so right, the session object doesn't have an id yet. attach can only associate objects with ids. The cascade logic inside the generated upsert seems to assume the session already has an id set, so it will only work if upsert is used for updating, which kinda defeats the purpose of an upsert method.

Workaround: I can override upsert to call super.upsert twice, once without cascade:

    if (cascade && model.id == null) {
      model.id = await super.upsert(model, cascade: false, only: only, onlyNonNull: onlyNonNull, isForeignKeyEnabled: isForeignKeyEnabled);
    }
    return await super.upsert(model, cascade: cascade, only: only, onlyNonNull: onlyNonNull, isForeignKeyEnabled: isForeignKeyEnabled);

Versions:

  jaguar_orm:
    dependency: "direct main"
    description:
      name: jaguar_orm
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.2.7"
  jaguar_orm_gen:
    dependency: "direct dev"
    description:
      name: jaguar_orm_gen
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.2.30"
  jaguar_query:
    dependency: transitive
    description:
      name: jaguar_query
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.2.9"
  jaguar_query_sqflite:
    dependency: "direct main"
    description:
      name: jaguar_query_sqflite
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.2.11"