I would like to know if there is any option for db_merge function to do UPSERT (update or insert) operation in table if key is "id" column of serial type (autoincrementing).
I need to insert new row if "id" is not defined, or update if "id" is specified. For now I have:
db_merge('t_project_proposal_writer') ->key(array('id' => $state['storage']['project']['proposal_id'])) ->fields(array( .... ))->execute();
$state['storage']['project']['proposal_id'] is null, because is not specified and it should create row with next id value. It is not working, returning:
Numeric value out of range: 7 ERROR: setval: value 0 is out of bounds for sequence "t_project_proposal_writer_id_seq"
It is working, when new id is manually specified, but then, how to get last inserted id of table? db_last_insert_id is not working in D7, only in D6.