Vincit / objection.js

An SQL-friendly ORM for Node.js
https://vincit.github.io/objection.js
MIT License
7.24k stars 635 forks source link

Not allowed to batch insert into a MySQL8 server #2217

Closed santiDotIO closed 2 years ago

santiDotIO commented 2 years ago

Now allowed to execute batch inserts on MySQL8 server, Works fine when running with knex directly, maybe mysql2 client is not getting grouped together with the mysql/postgress client?

The following query works just fine using knex directly

knex('plaid_account_meta')
    .insert([{
        account_id: '12345',
        type: 'abc'
    }, {
        account_id: '1574515',
        name: 'test'
    }])
    .onConflict('account_id')
    .merge();

but get the error below when I try to run it using Objectionjs

PlaidAccountMeta.query()
    .insert([{
        account_id: '12345',
        official_name: 'abc3',
    },{
        account_id: '1574515',
        name: 'testName3',
    }])
    .onConflict('account_id')
    .merge();
error: Error: batch insert only works with Postgresql and SQL Server
      at InsertOperation.onBefore2 (/home/node/app/node_modules/objection/lib/queryBuilder/operations/InsertOperation.js:31:13)
      at QueryBuilder.callAsyncOperationMethod (/home/node/app/node_modules/objection/lib/queryBuilder/QueryBuilderOperationSupport.js:387:39)
      at /home/node/app/node_modules/objection/lib/queryBuilder/QueryBuilder.js:1143:19

On the knex file:

client: 'mysql2',
version: '8.0',
koskimas commented 2 years ago

MySQL doesn't return the identifiers of the created rows. Objection models can't be created for the result without identifiers. That's why it's not supported.

santiDotIO commented 2 years ago

That makes sense. In my opinion, I believe there should still be some support, and instead of returning a set of models, just a count or boolean. Especially since this in a sort creates a weird scenario where it removes a feature of knex. I would imagine having this feature would still be more beneficial than having to break out of using objection to make a knex query? or at least some clearer explanation as to why in the docs or error messages.

vkruoso commented 1 year ago

This is really annoying issue, must agree with @santi6291. If we are using MySQL and want to insert batches, we are already not receiving any ids back, and that is fine. I understand that this might be necessary for some graph inserts, where you need to relate stuff to an object after it was inserted, but that is something that would be okay to not allow in MySQL. The only solution here is to bypass objection and use knex itself, that can lead to errors and other issues. @koskimas would you consider reopening this?