Skip to content

Fix last insert ID not being returned with postgresql. - #62

Closed
laacz wants to merge 1 commit into
j4mie:masterfrom
laacz:lastinsertid-postgres
Closed

laacz wants to merge 1 commit into
j4mie:masterfrom
laacz:lastinsertid-postgres

Conversation

@laacz

@laacz laacz commented Jul 4, 2012

Copy link
Copy Markdown
Contributor

When using PostgreSQL, upon saving new entries id is not being updated.

That's because PDO's implementation of lastInsertId requires sequence name as a parameter. One of three available options is fetching LASTVAL(). All three are described in the answer to this SO question.

I 'fixed' it by adding driver specific last_insert_id() method to ORM class.

@treffynnon

Copy link
Copy Markdown
Collaborator

I am thinking that RETURNING id is possibly the better of all the options there for this. I don't regularly use Postgres though so can anyone else chime in?

@treffynnon

Copy link
Copy Markdown
Collaborator

This feature is going to slip to version 1.4 as there has been no further input.

@laacz

laacz commented Dec 19, 2012

Copy link
Copy Markdown
Contributor Author

Thank you. I missed this notification from github. Answering your question - RETURNING would be an option, but this feature is present only since Postgres 9.1.

@treffynnon

Copy link
Copy Markdown
Collaborator

Is it likely that the servers will not have been upgraded to version 9.1? Ubuntu appear to have been running 9.1 as the default postgresql package for over a year now (Oneiric) although Debian are still 8.4 from what I can see.

@laacz

laacz commented Dec 19, 2012

Copy link
Copy Markdown
Contributor Author

I would bet that there is still a lot of 8.x servers out there. Some are just legacy systems, others probably do not upgrade for various reasons (plugin compatibility, syntax changes, no need to, etc). I would not rely on 9.1 being the minimum version available.

@treffynnon

Copy link
Copy Markdown
Collaborator

According to comments in the answer on the SO question that you linked to the RETURNING method works in 8.2.

In fact as I go back through documentation RETURNING appears to exists right back to version 8.2 of PostgreSQL.

@laacz

laacz commented Dec 19, 2012

Copy link
Copy Markdown
Contributor Author

@treffynnon Oh, then I read it diagonally. Relied on best answer. Then, of course, RETURNING is the way to go and falling back to SELECT from sequence only on older versions.

@treffynnon

Copy link
Copy Markdown
Collaborator

Is there anyway you could have a go at getting RETURNING to work as a pull request instead of LASTVAL()?

@laacz

laacz commented Dec 19, 2012

Copy link
Copy Markdown
Contributor Author

OK, I'll do that. That will be separate pull request then.

@treffynnon

Copy link
Copy Markdown
Collaborator

That would be perfect. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants