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

Generated Beans causing stack overflows #125

Closed IcarusComplex closed 5 years ago

IcarusComplex commented 5 years ago

Hello.

I'm having some issues when generating beans. These are my dependencies:

sqflite: ^1.1.5
jaguar_orm: ^2.2.6
jaguar_orm_gen: ^2.2.27
jaguar_query_sqflite: ^2.2.9
build_runner: 1.1.0 (Had to fix the version here to avoid dependency issues)

The scenario is as follows. I have a Bean, A, and a Bean B.

class A {
    @BelongsTo(BBean)
    string bId;
}

@GenBean()
class ABean extends Bean<A> with _ABean {
   final BBean bBean;
   ABean(Adapter adapter): bBean = BBean (adapter), super (adapter);

   String get tableName => "a_table";
}
class B{
    @HasMany(ABean)
    List<A> collectionOfA
}

@GenBean()
class BBean extends Bean<B> with _BBean {
   final ABean aBean;
   BBean (Adapter adapter): aBean = ABean (adapter), super (adapter);

   String get tableName => "b_table";
}

Pretty standard one to many relationship. In the setup above, if I attempt to instantiate any Beans I get a stack overflow as the constructors are calling one another. However, removing the reference to a BBean from ABean has the unwanted result that the auto generated _ABean expects a reference to BBean in its generated createTable method to get the tableName of BBean. Its easy enough to edit the code manually, but I feel like this isn't a clean solution.

Any way I can avoid this? Anything with my setup I need to change? Thank you!

Elvis5566 commented 5 years ago

Change one of your bean like this

  MyBean _myBean;
  MyBean get myBean => _myBean ??= MyBean(adapter);
IcarusComplex commented 5 years ago

Thank you @Elvis5566. This worked very nicely.

I recommend updating the OneToMany example code, as this implements it the way I had it above.

Seems an obvious fix now that I've seen. Thank you again!