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

Jonathan Buch john at oxyliquit.de
Wed Oct 25 08:55:59 EDT 2006


>> The problem is, that the frontend (Og) has to know the oid which has
>> been inserted.
> Why does the frontend need to know which oid has been inserted?
> When I think of an activity that requires insertion ( the creation of a
> new record in the DB ), I think in terms that all actions that need to
> be performed on the object are performed prior to .create .save, etc.
> I.E. at the point that I insert into the DB, that is the last action I
> expect to take on that object.  If I need to alter the record after it's
> created, then I expect to have to query for it first.

Lets use a simple example:

class OgModel
   proptery :name, String
   has_many OgModel

Now we create a instance and save it to the database with either of
those methods:

m = OgModel.create_with(:name => 'George')

m = OgModel.new
m.name = 'Stella'

After that statement, we have an object called 'm'.  When I now work on
this object (same object, not gotten anew from database):

m.name = 'Takeo'

What happens?  The .save method needs to UPDATE the model we just CREATEd
and it can only do this, if it can exactly identify the model.
So we need to know at this point which oid (or any other primary key) the
model has.

So, .save is by far not the last action, it is merely the first step.

But, taking your example that you use .save as the last, and querying
the database when you want to use the object further:
How (when you haven't got the primary key) do you know, that you get
exactly the object you want?

OgModel.create_with(:name => 'Chichi')
OgModel.find_with_oid(... what oid to use here?...)

Also, when working with relations, it would be mighty inconvenient to
not be able to work on the same object;  maybe adding a few other related
objects to a has_many for example (which must know the primary key for
insertion in the related objects) to a just created OgModel instance.

But maybe I'm just getting you wrong somehow..
Was my explanation right now enough to convince you that Og needs to know
the primary key before really inserting?  If not, I will try to think of
another way to explain this.


Feel the love

More information about the Nitro-general mailing list