Inserting empty array inserts a record with default values.
See original GitHub issueknex('test').insert({}).toString()
"insert into "test" default values"
knex('test').insert([{}]).toString()
"insert into "test" default values"
These two use cases make sense, but I think that the logic has incorrectly been extended to the following:
knex('test').insert([]).toString()
"insert into "test" default values"
Can I propose that inserting an empty array should either be a no-op or throw an error?
I can’t think of a case where an empty array would be intentionally used to to insert a single record. A no-op would be ideal, this way it saves the client checking for empty arrays on every insert.
At the moment I use this (shitty) workaround:
safeInsert = (k, table, rows, returning) ->
if _.isEmpty rows then [] else k(table).insert rows, returning or 'id'
Issue Analytics
- State:
- Created 9 years ago
- Comments:20 (15 by maintainers)
Top Results From Across the Web
PHP insert mysql NULL in empty array value for PDO insert
I have a pdo function that uses an array to add values to a table. It adds perfectly, my issue is that if...
Read more >Inserting JSON Data with Default Values in PostgreSQL
DETAIL: Array value must start with "{" or dimension information. So close! The problem seems to be that the default empty array value...
Read more >Set default values for fields or controls - Microsoft Support
This article explains how to set a default value for a table field or for a control on a form in an Access...
Read more >How to Insert Data Into an Array in PostgreSQL - PopSQL
There are two accepted syntaxes for inserting data to an array column. The first uses ARRAY [value1, value2, etc]: insert into contacts (firstname, ......
Read more >5 PL/SQL Collections and Records
To add elements to an empty collection, invoke the EXTEND method ... Using a key-value pair for the first time adds that pair...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If anyone stumbles upon this, the only way this works is by inserting one element at a time with:
IMO
.insert([{},{}]);
makes sense (although it doesn’t work);Closing this as the original issue has already been adjusted by Tim into a
noop
long ago.Unfortunately the
noop
in this case is an empty SQL string sent to db. Imo a realnoop
would imply no database connection at all, but that can be a potential improvement.