diff --git a/angel_orm_generator/lib/src/orm_build_context.dart b/angel_orm_generator/lib/src/orm_build_context.dart index 8ac2b5fe..8a94dcb8 100644 --- a/angel_orm_generator/lib/src/orm_build_context.dart +++ b/angel_orm_generator/lib/src/orm_build_context.dart @@ -236,6 +236,16 @@ Future buildOrmContext( localKey ??= '${rcc.snakeCase}_$foreignKey'; } + // Figure out the join type. + var joinType = JoinType.left; + var joinTypeRdr = cr.peek('joinType')?.objectValue; + if (joinTypeRdr != null) { + var idx = joinTypeRdr.getField('index')?.toIntValue(); + if (idx != null) { + joinType = JoinType.values[idx]; + } + } + var relation = RelationshipReader( type, localKey: localKey, @@ -245,6 +255,7 @@ Future buildOrmContext( through: through, foreign: foreign, throughContext: throughContext, + joinType: joinType ?? JoinType.left, ); // print('Relation on ${buildCtx.originalClassName}.${field.name} => ' diff --git a/angel_orm_generator/lib/src/readers.dart b/angel_orm_generator/lib/src/readers.dart index 5c251dd1..a1039514 100644 --- a/angel_orm_generator/lib/src/readers.dart +++ b/angel_orm_generator/lib/src/readers.dart @@ -34,6 +34,7 @@ class RelationshipReader { final DartType through; final OrmBuildContext foreign; final OrmBuildContext throughContext; + final JoinType joinType; const RelationshipReader(this.type, {this.localKey, @@ -42,11 +43,14 @@ class RelationshipReader { this.cascadeOnDelete, this.through, this.foreign, - this.throughContext}); + this.throughContext, + this.joinType = JoinType.left}); bool get isManyToMany => type == RelationshipType.hasMany && throughContext != null; + String get joinTypeString => joinType.toString().replaceAll('JoinType.', ''); + FieldElement findLocalField(OrmBuildContext ctx) { return ctx.effectiveFields.firstWhere( (f) => ctx.buildContext.resolveFieldName(f.name) == localKey, diff --git a/angel_orm_test/lib/src/models/book.dart b/angel_orm_test/lib/src/models/book.dart index 1d515c67..b89b171d 100644 --- a/angel_orm_test/lib/src/models/book.dart +++ b/angel_orm_test/lib/src/models/book.dart @@ -9,10 +9,10 @@ part 'book.g.dart'; @serializable @orm class _Book extends Model { - @belongsTo + @BelongsTo(joinType: JoinType.inner) _Author author; - @BelongsTo(localKey: "partner_author_id") + @BelongsTo(localKey: "partner_author_id", joinType: JoinType.inner) _Author partnerAuthor; String name;