schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
66 stars 16 forks source link

Self-join relations #49

Closed f3l1x98 closed 1 year ago

f3l1x98 commented 1 year ago

I am having the issue of trying to create a 1:1 relation between the same table.

@Model()
abstract class Game {
  @PrimaryKey()
  @AutoIncrement()
  int get id;

  // [...]

  Game? get prequel;
  //Game? get sequel;  // GameA.sequel = GameB.prequel
}

This will generate a schema with two prequelIds (probably due to cyclic issues, because none are a List). If I also add the sequel it will result into three prequelIds and one sequelId.

Not sure, but perhaps this could be fixed by adding somekind of Join annotation (similar to NestJs Typeorm, Spring Boot JPA, ...) that allows specifying the join column/attribute:

@Model()
abstract class Game {
  @PrimaryKey()
  @AutoIncrement()
  int get id;

  // [...]

  @Join('sequel')
  Game? get prequel;
  @Join('prequel')
  Game? get sequel;
}

This annotation could also be expanded to allow joins on non-primary key columns.

schultek commented 1 year ago

I have never considered self-joins. I will try to add support for it.

schultek commented 1 year ago

Self-Joins as well as a new @BindTo annotation are added in the v0.12.0 release.