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();

```

where `$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](https://www.drupal.org/node/729970) is not working in D7, only in D6.