Skip to content

Commit

Permalink
fix(core): exclude collections from returning clause from em.upsert
Browse files Browse the repository at this point in the history
Closes #4382
  • Loading branch information
B4nan committed May 23, 2023
1 parent 6cc708c commit e342449
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {

if (!helper(entity).hasPrimaryKey()) {
const pk = await this.driver.findOne(meta.className, where, {
fields: meta.hydrateProps.filter(p => !p.lazy && !(p.name in data!)).map(p => p.name) as EntityField<Entity>[],
fields: meta.comparableProps.filter(p => !p.lazy && !(p.name in data!)).map(p => p.name) as EntityField<Entity>[],
ctx: em.transactionContext,
convertCustomTypes: true,
});
Expand Down Expand Up @@ -806,7 +806,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
}

const pks = await this.driver.find(meta.className, { $or: [...loadPK.values()] as Dictionary[] }, {
fields: meta.hydrateProps.filter(p => !p.lazy && !(p.name in data![0] && !add.has(p.name))).map(p => p.name),
fields: meta.comparableProps.filter(p => !p.lazy && !(p.name in data![0] && !add.has(p.name))).map(p => p.name),
ctx: em.transactionContext,
convertCustomTypes: true,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
qb.insert(data as T)
.onConflict(uniqueFields.map(p => meta?.properties[p]?.fieldNames[0] ?? p))
.merge(Object.keys(data).filter(f => !uniqueFields.includes(f)))
.returning(meta?.hydrateProps.filter(p => !p.lazy && !(p.name in data)).map(p => p.name) ?? '*');
.returning(meta?.comparableProps.filter(p => !p.lazy && !(p.name in data)).map(p => p.name) ?? '*');
} else {
qb.update(data).where(where);
}
Expand All @@ -452,7 +452,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
qb.insert(data)
.onConflict(uniqueFields.map(p => meta.properties[p]?.fieldNames[0] ?? p))
.merge(Object.keys(data[0]).filter(f => !uniqueFields.includes(f)))
.returning(meta.hydrateProps.filter(p => !p.lazy && !(p.name in data[0])).map(p => p.name) ?? '*');
.returning(meta.comparableProps.filter(p => !p.lazy && !(p.name in data[0])).map(p => p.name) ?? '*');
return qb.execute('run', false);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/features/upsert/upsert.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MikroORM, Entity, PrimaryKey, ManyToOne, Property, SimpleLogger, Unique, Ref, ref, EventSubscriber, EventArgs } from '@mikro-orm/core';
import {
MikroORM, Entity, PrimaryKey, ManyToOne, Property, SimpleLogger,
Unique, Ref, ref, EventSubscriber, EventArgs, OneToMany, Collection,
} from '@mikro-orm/core';
import { mockLogger } from '../../helpers';

@Entity()
Expand All @@ -15,6 +18,9 @@ export class Author {
@Property({ name: 'current_age' })
age: number;

@OneToMany(() => Book, b => b.author)
books = new Collection<Book>(this);

constructor(email: string, age: number) {
this.email = email;
this.age = age;
Expand Down

0 comments on commit e342449

Please sign in to comment.