question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inserting empty array inserts a record with default values.

See original GitHub issue
knex('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:closed
  • Created 9 years ago
  • Comments:20 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
PierBovercommented, Jun 19, 2017

If anyone stumbles upon this, the only way this works is by inserting one element at a time with:

.insert({});
.insert({});

IMO .insert([{},{}]); makes sense (although it doesn’t work);

0reactions
wubzzcommented, Feb 16, 2018

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 real noop would imply no database connection at all, but that can be a potential improvement.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found