avoid duplicated records with drupal_write_record I want to test drupal_write_record, the insert part is ok, but this function seem to duplicate records.

How to make a condition for if new record is already stored ?

some like db_merge with key

Thank you
This function can take 3 arguments , in this situation the third argument acts as an array of primary keys and if we pass the third argument to the function (primary key), it will update the record, if don't pass - it will insert a new record .