Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:24:51+00:00 2026-05-24T10:24:51+00:00

When using db_merge, it does not seem to be possible to retrieve the last

  • 0

When using db_merge, it does not seem to be possible to retrieve the last insert ID if an INSERT was made.

MergeQuery::execute() returns MergeQuery::STATUS_INSERT if an insert was made, not the insert ID as InsertQuery::execute() does.

Is there an elegant way to retrieve the last insert ID when using db_merge?

Sources:
http://api.drupal.org/api/drupal/includes–database–query.inc/function/MergeQuery%3A%3Aexecute/7
http://api.drupal.org/api/drupal/includes–database–query.inc/function/MergeQuery%3A%3Aexecute/7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:24:52+00:00Added an answer on May 24, 2026 at 10:24 am

    Edit: Solved. modules/simpletest/tests/database_test.test contains a solution where we requery the database using known values to retrieve the record and fetch it’s ID. Still seems odd there’s no easier way to get the last insert ID, but this works for my purposes.

    $result = db_merge('test_people')
      ->key(array('job' => 'Presenter'))
      ->fields(array(
        'age' => 31,
        'name' => 'Tiffany',
      ))
      ->execute();
    $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Presenter'))->fetch();
    $last_insert_id = $person->id; // Core test does not actually include an ID, but this is how it could work
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am importing csv to db using bulk insert. It is the comma delimited
Could someone explain me what will happen when I execute this query I'm using
Using a DB with C++ is a real mess and it was refreshing when
I am using oracle DB to maintain more than 30 tables, how can I
I'm considering using Berkeley DB to cache some data on an application cluster. What's
I'm using a DB table with various different entities. This means that I can't
I want to connect to DB using the iSeries Client Access driver. I use
When I say this using (Entities db = new Entities()) { return db.TableName.AsQueryable().ToList(); }
I am interacting with an Oracle DB using SQL over an ODBC connection. Is
I need to convert a flat file to DB using MS SSIS. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.