Skip to content

Commit

Permalink
fix(core): return DriverException from em.upsertMany()
Browse files Browse the repository at this point in the history
Closes #4897
  • Loading branch information
B4nan committed Nov 5, 2023
1 parent fea5acb commit 1ebfbdd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
qb.ignore();
}

return qb.execute('run', false);
return this.rethrow(qb.execute('run', false));
}

const collections = options.processCollections ? data.map(d => this.extractManyToMany(entityName, d)) : [];
Expand Down
29 changes: 26 additions & 3 deletions tests/EntityManager.postgre.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { v4 } from 'uuid';
import type { EventSubscriber, ChangeSet, AnyEntity, FlushEventArgs, FilterQuery } from '@mikro-orm/core';
import {
Collection, Configuration, EntityManager, LockMode, MikroORM, QueryFlag, QueryOrder, Reference, ValidationError, ChangeSetType, wrap, expr,
UniqueConstraintViolationException, TableNotFoundException, NotNullConstraintViolationException, TableExistsException, SyntaxErrorException,
NonUniqueFieldNameException, InvalidFieldNameException, LoadStrategy, IsolationLevel, PopulateHint, ref,
Collection,
Configuration,
EntityManager,
LockMode,
MikroORM,
QueryFlag,
QueryOrder,
Reference,
ValidationError,
ChangeSetType,
wrap,
UniqueConstraintViolationException,
TableNotFoundException,
NotNullConstraintViolationException,
TableExistsException,
SyntaxErrorException,
NonUniqueFieldNameException,
InvalidFieldNameException,
LoadStrategy,
IsolationLevel,
PopulateHint,
ref,
expr,
ForeignKeyConstraintViolationException,
} from '@mikro-orm/core';
import { PostgreSqlDriver, PostgreSqlConnection } from '@mikro-orm/postgresql';
import { Address2, Author2, Book2, BookTag2, FooBar2, FooBaz2, Publisher2, PublisherType, PublisherType2, Test2, Label2 } from './entities-sql';
Expand Down Expand Up @@ -1822,6 +1843,8 @@ describe('EntityManagerPostgre', () => {
test('exceptions', async () => {
const driver = orm.em.getDriver();
await driver.nativeInsert(Author2.name, { name: 'author', email: 'email' });
await expect(orm.em.upsert(Author2, { name: 'author', email: 'email', favouriteAuthor: 123 })).rejects.toThrow(ForeignKeyConstraintViolationException);
await expect(orm.em.upsertMany(Author2, [{ name: 'author', email: 'email', favouriteAuthor: 123 }])).rejects.toThrow(ForeignKeyConstraintViolationException);
await expect(driver.nativeInsert(Author2.name, { name: 'author', email: 'email' })).rejects.toThrow(UniqueConstraintViolationException);
await expect(driver.nativeInsert(Author2.name, {})).rejects.toThrow(NotNullConstraintViolationException);
await expect(driver.nativeInsert('not_existing', { foo: 'bar' })).rejects.toThrow(TableNotFoundException);
Expand Down

0 comments on commit 1ebfbdd

Please sign in to comment.