[Nitro] question about postgresql.rb && last_insert_id and insert_sql

Jonathan Buch john at oxyliquit.de
Tue Oct 24 04:04:24 EDT 2006


> could someone explain to me the reasoning behind the following code in
> postgresql.rb.  If the klass.primary_key is a sequence, there is no need
> to explicitly set the primary key value in PostgreSQL, PG will
> automatically do it. You can do this by either not passing a value for
> that column, or by passing default or DEFAULT.  This would make
> insert_sql a less costly call.  What am I missing that requires the code
>  to be written as it currently is??

The problem is, that the frontend (Og) has to know the oid which has
been inserted.

> --this comment is incorrect -- select nextval() does just that, advances
> the sequence and returns the next value ( it could be re-written as #
> Return what will become the last inserted row id. )
>   # Return the last inserted row id.
>   def last_insert_id(klass)

Yes, this comment is misleading.  The function was copied from the
generic sql adapter.  I will see to it that there is a better comment
the next release.

When using 'default' instead of the actual id (gotten by the sequence):

The problem here is, that you have no way of telling after the insert,
which oid has been selected.  The frontend has to know this, to identify
the object.
Unless you know some clever way of really getting the 'last insert id'
which is quite... unfortunate anyway, what about many many inserts at
once and the frontend trying to keep up with all that or doing clever
blocking with transactions....

> test=# insert into seq_test
> values('uname','fname','lname','emailaddr',default);

haha, manv will laugh at me for that, but... Thank you for the 'default'
keyword, I didn't know it.  XD


More information about the Nitro-general mailing list