Skip to content

Commit

Permalink
fix(core): skip 1:1 owner auto-joins for lazy properties
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Apr 10, 2024
1 parent ccdf018 commit 6442e57
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection

const relationsToPopulate = populate.map(({ field }) => field.split(':')[0]);
const toPopulate: PopulateOptions<T>[] = meta.relations
.filter(prop => prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && !relationsToPopulate.includes(prop.name))
.filter(prop => prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && !prop.lazy && !relationsToPopulate.includes(prop.name))
.filter(prop => fields.length === 0 || fields.some(f => prop.name === f || prop.name.startsWith(`${String(f)}.`)))
.map(prop => ({ field: `${prop.name}:ref` as any, strategy: prop.strategy }));

Expand Down Expand Up @@ -1541,7 +1541,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
if (!options.fields.includes('*') && !options.fields.includes(`${qb.alias}.*`)) {
ret.unshift(...meta.primaryKeys.filter(pk => !options.fields!.includes(pk)));
}
} else if (!Utils.isEmpty(options.exclude) || lazyProps.some(p => !p.formula)) {
} else if (!Utils.isEmpty(options.exclude) || lazyProps.some(p => !p.formula && (p.kind !== '1:1' || p.owner))) {
const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, options.exclude as string[], false));
ret.push(...props.filter(p => !lazyProps.includes(p)).map(p => p.name));
addFormulas = true;
Expand Down
1 change: 0 additions & 1 deletion tests/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export async function initORMMsSql(additionalOptions: Partial<Options<MsSqlDrive
password: 'Root.Root',
debug: true,
forceUtcTimezone: true,
autoJoinOneToOneOwner: false,
logger: i => i,
extensions: [Migrator, SeedManager, EntityGenerator],
...additionalOptions,
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-mssql/Author2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Author2 extends BaseEntity2 {
@OneToMany({ entity: () => Book2, mappedBy: 'author', strategy: LoadStrategy.JOINED, orderBy: { title: QueryOrder.ASC } })
books2 = new Collection<Book2>(this);

@OneToOne({ entity: () => Address2, mappedBy: address => address.author, cascade: [Cascade.ALL] })
@OneToOne({ entity: () => Address2, mappedBy: address => address.author, cascade: [Cascade.ALL], lazy: true })
address?: Address2;

@ManyToMany({ entity: () => Author2, pivotTable: 'author_to_friend' })
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-mssql/Book2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Book2 {
@ManyToOne(() => Publisher2, { cascade: [Cascade.PERSIST, Cascade.REMOVE], nullable: true, ref: true })
publisher?: Ref<Publisher2>;

@OneToOne({ cascade: [], mappedBy: 'book', nullable: true })
@OneToOne({ cascade: [], mappedBy: 'book', nullable: true, lazy: true })
test?: Test2;

@ManyToMany({ entity: () => BookTag2, cascade: [], fixedOrderColumn: 'order' })
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-mssql/FooBaz2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FooBaz2 {
@Property()
name: string;

@OneToOne(() => FooBar2, 'baz')
@OneToOne(() => FooBar2, 'baz', { lazy: true })
bar?: FooBar2;

@Property({ version: true })
Expand Down

0 comments on commit 6442e57

Please sign in to comment.