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

  • Home
  • SEARCH
  • 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

Is there a way to execute a query(containing built in DB function) using PreparedStatement?
I'm using TransactionScope to do some batch insert and updates. Problem is, I'm getting
Using a DB with C++ is a real mess and it was refreshing when
I want to connect to DB using the iSeries Client Access driver. I use
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
I'm trying to write someting into the DB using PHP but if I try
I am looking to manage a SQL Server 2008 DB using Management Studio 2005.
We're deploying some new WCF calls in our SQL 2005 DB using the CLR.
Using LINQ to SQL db.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList(); executes SELECT [t1].[ID], [t1].[CategoryID], [t1].[Name],

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.