Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when cascading upsert #173

Open
outofculture opened this issue Dec 24, 2019 · 0 comments
Open

Error when cascading upsert #173

outofculture opened this issue Dec 24, 2019 · 0 comments

Comments

@outofculture
Copy link

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant