Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertMany is not reading schema option #4424

Closed
wayn3r opened this issue Jun 6, 2023 · 7 comments
Closed

insertMany is not reading schema option #4424

wayn3r opened this issue Jun 6, 2023 · 7 comments
Milestone

Comments

@wayn3r
Copy link

wayn3r commented Jun 6, 2023

Describe the bug
If I try to use EntityManager.insertMany method, with { schema: "<<runtime calculated valid schema>>" }, the schema option is ignore

Stack trace

at PostgreSqlExceptionConverter.convertException ("<base-dir>"\node_modules\.pnpm\@mikro-orm+postgresql@5.7.11_@mikro-orm+core@5.7.11\node_modules\@mikro-orm\postgresql\PostgreSqlExceptionConverter.js:38:24)
    at PostgreSqlDriver.convertException ("<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:197:54)
    at "<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:201:24
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at PostgreSqlDriver.nativeInsertMany ("<base-dir>"\node_modules\.pnpm\@mikro-orm+knex@5.7.11_timz64mvbvdsmbdwuyrjmo7tfa\node_modules\@mikro-orm\knex\AbstractSqlDriver.js:303:21)
    at ChangeSetPersister.persistNewEntity ("<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\unit-of-work\ChangeSetPersister.js:85:21)
    at ChangeSetPersister.executeInserts ("<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\unit-of-work\ChangeSetPersister.js:29:13)
    at ChangeSetPersister.runForEachSchema ("<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\unit-of-work\ChangeSetPersister.js:68:13)
    at SqlEntityManager.insertMany ("<base-dir>"\node_modules\.pnpm\@mikro-orm+core@5.7.11_zmp53sb22yqva5imfl74cthtbm\node_modules\@mikro-orm\core\EntityManager.js:784:13)

To Reproduce
Steps to reproduce the behavior:

  1. Create an Entity with the schema set to *, e.g.: CustomerEntity
  2. Create a list of instances of that entity, e.g.: const customers = [new CustomerEntity(), new CustomerEntity()]
  3. Use the entity manager to insert them all, em.insertMany(CustomerEntity, customers, { schema: "<<runtime calculated valid schema>>" })

Expected behavior
This should insert all customers with the given schema

Versions

Dependency Version
@mikro-orm/core 5.7.11
@mikro-orm/postgresql 5.7.11
node v18.12.1
typescript 4.9.5
@B4nan
Copy link
Member

B4nan commented Jun 6, 2023

The option should be taken into account only for POJOs, so em.nativeInsert(Entity, [data1, data]), not for existing instances - for those you need to set them via wrap(entity).setSchema(schema) or create the instances via em.create(Entity, data, { schema }). But the POJO signature will do that for you in one step:

const customers = await em.insertMany(CustomerEntity, [{}, {}], { schema: '...' });

Alternative to that would be:

const customers = [em.create(CustomerEntity, {}, { schema: '...' }), em.create(CustomerEntity, {}, { schema: '...' })];
await em.insertMany(customers); // no need to specify the entity type with entity instances

Let me know if that works.

@wayn3r
Copy link
Author

wayn3r commented Jun 7, 2023

@B4nan this didn't work for me, nativeInsert doesn't accept an array as second argument it has to be a single entity.
But I solved this with a workaround, I just used repository.upsertMany instead. This method does use the schema. I think the insertMany should also do it.

@B4nan
Copy link
Member

B4nan commented Jun 7, 2023

Sorry, it's insertMany, not nativeInsert, I must have copy pasted that from somewhere.

@wayn3r
Copy link
Author

wayn3r commented Sep 19, 2023

I tried it today and it worked with plain JS objects. Closing this issue.

@wayn3r wayn3r closed this as completed Sep 19, 2023
@B4nan
Copy link
Member

B4nan commented Sep 19, 2023

I actually changed my mind on this, planing to change this in v6 so the schema option will work with entities (that do not have some schema already set) too, so let's keep it open.

@B4nan B4nan reopened this Sep 19, 2023
@B4nan B4nan added this to the 6.0 milestone Sep 19, 2023
@wayn3r
Copy link
Author

wayn3r commented Sep 19, 2023

Great

@B4nan
Copy link
Member

B4nan commented Sep 20, 2023

Fixed in v6 via e929c30?w=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants