Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wishlist: Return last insert ID after quick_insert. #93

Open
jahagirdar opened this issue Aug 19, 2017 · 2 comments
Open

Wishlist: Return last insert ID after quick_insert. #93

jahagirdar opened this issue Aug 19, 2017 · 2 comments

Comments

@jahagirdar
Copy link

Everywhere in my code I end up doing the following.

 database->quick_insert($table,$data);
     $data->{id}=database->last_insert_id(undef,undef,undef,$pk);
     if ($data->{id}==0){
        my $data=database->quick_select($table,$data);
                }

it would be much cleaner to replace the above with either one of the following

$data=database->quick_insert($table,$data,{return=>$pk});
OR
$data->{id}=database->quick_insert($table,$data,{return=>$pk});
OR
$data->{id}=database->quick_insert_with_id($table,$data,{return=>$pk});

@1nickt
Copy link

1nickt commented Nov 4, 2017

Hi,

last_insert_id is not supported by every DBI driver, and support is inconsistent among those that offer it.

This is how I do it for a MySQL DB:

get '/insert/:name' => sub {
    database->quick_insert('people', { name => params->{name} });
    my $id = ( database->selectall_array('select last_insert_id()') )[0][0];
};

@sonntagd
Copy link

I created the quite simple module DBIx::Core::Handle::ReturnValue which can be used together with Dancer2::Plugin::Database. In addition to simplifying the use of last_insert_id a bit, it can be very helpful when using the Postgres RETURNING - which can also be used with UUIDs or other non-autoincrement primary keys.

This functionality could also be integrated directly into Dancer::Plugin::Database::Core::Handle. And there are already other functions in the module which are Postgres-specific (e.g. ilike).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants