When inserting zero rows into an empty table (as can happen with INSERT FROM SELECT), the default $options['return'] value of Database::RETURN_INSERT_ID can cause an error in Postgres after the INSERT has (successfully) competed:

'SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR: currval of sequence "(table)_(column)_seq" is not yet defined in this session'

In particular this can block the taxonomy module's upgrade path: http://drupal.org/node/1759144#comment-6828288 (taxonomy_update_7005() creates a new table and then inserts into it), but it might potentially cause problems elsewhere as well.

Setting an explicit return option of Database::RETURN_NULL fixes this, and can safely be done everywhere that the return value of the db_insert() is not needed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jweowu’s picture

Status: Active » Needs review
FileSize
4.15 KB

This patch covers all core instances of the text "->from(", save for a non-postgres instance in ./includes/database/sqlite/schema.inc:386 which I've left alone.

Status: Needs review » Needs work

The last submitted patch, drupal-db_insert_return_null-1993166-1.patch, failed testing.

jweowu’s picture

Status: Needs work » Needs review
FileSize
4.14 KB

Oops, that didn't apply to HEAD. Re-rolling.

jweowu’s picture

...and I managed to get a tab into the indentation as well, so fixing that: