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

upsert: critical error in update #183

Closed jamespet77 closed 4 years ago

jamespet77 commented 4 years ago

build_runner is not creating the function correctly for the update. I have included a section of generated code.

if "isForeignKeyEnabled" the retId is getting set to Future instead of int and is causing an fatal error in cascade. I have ** highlighted the sections.

Future<dynamic> upsert(Node model,
      {bool cascade = false,
      Set<String> only,
      bool onlyNonNull = false,
      isForeignKeyEnabled = false}) async {
    var retId;
    if (isForeignKeyEnabled) {
      final Insert insert = Insert(tableName, ignoreIfExist: true)
          .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull));
      retId = await adapter.insert(insert);
      if (retId == null) {
        final Update update = updater
            .where(this.id.eq(model.id))
            .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull));
        **retId = adapter.update(update);**.  <-   missing await 
      }
    } else {
      final Upsert upsert = upserter
          .setMany(toSetColumns(model, only: only, onlyNonNull: onlyNonNull))
          .id(id.name);
      retId = await adapter.upsert(upsert);
    }
    if (cascade) {
      Node newModel;
      if (model.properties != null) {
        **newModel ??= await find(retId);**.   <- fatal Error. 
        model.properties
            .forEach((x) => nodePropertyBean.associateNode(x, newModel));
        for (final child in model.properties) {
          await nodePropertyBean.upsert(child, cascade: cascade);
        }
      }
      if (model.scenes != null) {
        newModel ??= await find(retId);
        model.scenes.forEach((x) => sceneMemberBean.associateNode(x, newModel));
        for (final child in model.scenes) {
          await sceneMemberBean.upsert(child, cascade: cascade);
        }
      }
    }
    return retId;
  }
jamespet77 commented 4 years ago

Wrong package.