commit
ca36d2d5dc
3 changed files with 13 additions and 8 deletions
|
@ -229,19 +229,20 @@ class PostgresOrmGenerator extends GeneratorForAnnotation<ORM> {
|
||||||
buf.write(ctx.prefix + "$name");
|
buf.write(ctx.prefix + "$name");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
int relationCounter = 0;
|
||||||
// Add all relationship fields...
|
// Add all relationship fields...
|
||||||
for (var name in ctx.relationships.keys) {
|
for (var name in ctx.relationships.keys) {
|
||||||
// Should only run when a JOIN is performed, i.e. singular
|
// Should only run when a JOIN is performed, i.e. singular
|
||||||
var relationship = ctx.populateRelationship(name);
|
var relationship = ctx.populateRelationship(name);
|
||||||
|
|
||||||
if (relationship.isSingular) {
|
if (relationship.isSingular) {
|
||||||
var modelTypeContext = await relationship.modelTypeContext;
|
var modelTypeContext = await relationship.modelTypeContext;
|
||||||
modelTypeContext.fields.forEach((f) {
|
modelTypeContext.fields.forEach((f) {
|
||||||
if (i++ > 0) buf.write(', ');
|
if (i++ > 0) buf.write(', ');
|
||||||
var name = modelTypeContext.resolveFieldName(f.name);
|
var name = modelTypeContext.resolveFieldName(f.name);
|
||||||
buf.write('${relationship.foreignTable}.$name');
|
buf.write('rel$relationCounter.$name');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
relationCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
|
@ -265,17 +266,16 @@ class PostgresOrmGenerator extends GeneratorForAnnotation<ORM> {
|
||||||
|
|
||||||
var relationsIfThen = ifThen(prefix.equals(literal(null)));
|
var relationsIfThen = ifThen(prefix.equals(literal(null)));
|
||||||
|
|
||||||
|
int relationCounter = 0;
|
||||||
// Apply relationships
|
// Apply relationships
|
||||||
ctx.relationships.forEach((name, r) {
|
ctx.relationships.forEach((name, r) {
|
||||||
var relationship = ctx.populateRelationship(name);
|
var relationship = ctx.populateRelationship(name);
|
||||||
|
|
||||||
if (relationship.isSingular) {
|
if (relationship.isSingular) {
|
||||||
String b = ' LEFT OUTER JOIN ${relationship.foreignTable} ON ${ctx
|
String b = ' LEFT OUTER JOIN ${relationship.foreignTable} AS rel$relationCounter ON ${ctx
|
||||||
.tableName}.${relationship.localKey} = ${relationship
|
.tableName}.${relationship.localKey} = rel$relationCounter.${relationship.foreignKey}';
|
||||||
.foreignTable}.${relationship.foreignKey}';
|
|
||||||
relationsIfThen.addStatement(buf.invoke('write', [literal(b)]));
|
relationsIfThen.addStatement(buf.invoke('write', [literal(b)]));
|
||||||
}
|
}
|
||||||
|
relationCounter++;
|
||||||
// A join-based solution won't work for hasMany and co.
|
// A join-based solution won't work for hasMany and co.
|
||||||
/*else {
|
/*else {
|
||||||
String b = ' LEFT OUTER JOIN ${relationship.foreignTable} ON ${ctx
|
String b = ' LEFT OUTER JOIN ${relationship.foreignTable} ON ${ctx
|
||||||
|
|
|
@ -12,6 +12,7 @@ import 'common.dart';
|
||||||
main() {
|
main() {
|
||||||
PostgreSQLConnection connection;
|
PostgreSQLConnection connection;
|
||||||
Author rowling;
|
Author rowling;
|
||||||
|
Author jameson;
|
||||||
Book deathlyHallows;
|
Book deathlyHallows;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
|
@ -19,10 +20,11 @@ main() {
|
||||||
|
|
||||||
// Insert an author
|
// Insert an author
|
||||||
rowling = await AuthorQuery.insert(connection, name: 'J.K. Rowling');
|
rowling = await AuthorQuery.insert(connection, name: 'J.K. Rowling');
|
||||||
|
jameson = await AuthorQuery.insert(connection, name: 'J.K. Jameson');
|
||||||
|
|
||||||
// And a book
|
// And a book
|
||||||
deathlyHallows = await BookQuery.insert(connection,
|
deathlyHallows = await BookQuery.insert(connection,
|
||||||
authorId: int.parse(rowling.id), name: 'Deathly Hallows');
|
authorId: int.parse(rowling.id), name: 'Deathly Hallows', partnerAuthorId: int.parse(jameson.id));
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() => connection.close());
|
tearDown(() => connection.close());
|
||||||
|
|
|
@ -12,6 +12,9 @@ class _Book extends Model {
|
||||||
@belongsTo
|
@belongsTo
|
||||||
Author author;
|
Author author;
|
||||||
|
|
||||||
|
@BelongsTo(localKey: "partner_author_id")
|
||||||
|
Author partnerAuthor;
|
||||||
|
|
||||||
int authorId;
|
int authorId;
|
||||||
String name;
|
String name;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue