API page: http://api.drupal.org/api/drupal/includes!database!database.inc/function...
Drupal version : 7.18

While trying to migrate from drupal_write_record() to db_insert() / db_update(), discovered that db_insert() returns a weird key instead of the last inserted key.

I have standard empty 'accesslog' table that never been used for the example.

$data['timestamp'] = REQUEST_TIME;
$id = db_insert('accesslog')->fields($data)->execute();
dsm($id, 'db_insert');
$data = new stdClass();
$data->timestamp = REQUEST_TIME;
drupal_write_record('accesslog', $data);
dsm($data->aid, 'drupal_write_record');

what we get:
$id = 43587111;
$data->aid = 2;

So you can see that drupal_write_record() returns the correct last id, while db_insert() returns some weird key.

@see screenshot attached

CommentFileSizeAuthor
Screenshot - 05232013 - 01:21:33 PM.png21.93 KBalechko

Comments

alechko’s picture

Version: 7.x-dev » 7.18
Component: documentation » database system

fixing version and component info

avpaderno’s picture

Version: 7.18 » 7.x-dev

Bugs are always fixed in the development snapshot.

Erich Schulz’s picture

Issue summary: View changes

just experienced this

I'm on mysql and core is uptodate

I'm inserting to a custom table without a schema definition - the record inserts ok

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.