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 52 forks source link

Cascaded detach of ManyToMany related objects does not work if there are no objects in relationship #99

Closed MartinHlavna closed 5 years ago

MartinHlavna commented 5 years ago

Hello, I have exprienced issue with following model. I am using jaguar_query_sqflite as adapter.

  1. Consider two entity models Post and Tag with ManyToMany relationship.
  2. In jaguar_orm, this results in 3 objects. Post, Tag and PostTag object representing single relation between Post and Tag
  3. Generated beans using jaguar_orm_gen are _PostBean, _TagBean and _PostTagBean
  4. _PostBean contains method remove with cascade named parameter
  5. If cascade = true, _PostBean calls postTagBean.detachPost(newModel);
  6. postTagBean.detachPost has following code
    Future<int> detachPost(Post model) async {
    final dels = await findByPost(model.id);
    await removeByPost(model.id);
    final exp = Or();
    for (final t in dels) {
      exp.or(tagBean.id.eq(t.tagId));
    }
    return await tagBean.removeWhere(exp);
    }
  7. Hovewer, if relationship is optional and we want to remove Post with no Tags this results in generating empty Or which is not valid SQL
MartinHlavna commented 5 years ago

PR #100 should fix this

tejainece commented 5 years ago

Published as jaguar_orm_gen version 2.2.24.