http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L239

On 23 сент, 19:08, Siah <[EMAIL PROTECTED]> wrote:
> My examples was faulty. Sorry. I meant after you save the object for
> the first time, that is:
>
> obj = Product(name='Apple')
> obj.save()
>
> At this point obj has an ID in it. How does it know that ID?
>
> A SQL statement similar to the following must be generated:
> insert into product_table (id, name) values(next_sequence, 'Apple');
>
> But that SQL statement doesn't return its generated ID. Is it that
> Django first gets the next ID in sequence and then uses it in insert
> or it retrieves it in a different way.
>
> As Alex mentioned above, if there is a select statement, what could it
> be? (Select id from product_table where name='Apple') doesn't seem
> plausible.
>
> Once again, I have an AFTER trigger that sets my PKs from a different
> table and need to understand how django does it, so I can understand
> how to approach my own problem,
>
> Thanks again,
> Sia
>
> On Sep 23, 5:58 pm, Richard Dahl <[EMAIL PROTECTED]> wrote:
>
> > Actually,
> > from the db-api documentation.
>
> > "To create an object, instantiate it using keyword arguments to the
> > model class, then call save() to save it to the database. ...
>
> > ... Django doesn't hit the database until you explicitly call save()."
>
> > The code:
> > obj = Product(name='Apple')
>
> > obj.id  will not be set until you either explicitly set it, or call
> > save(), at which time django will utilize the lack of a pk (among
> > other things) to determine whether or not to do an insert or update.
>
> > -richard
>
> > On Sep 23, 2007, at 9:38 AM, Alex Koshelev wrote:
>
> > > I think that when you create Product object django inserts new row
> > > into database and retrieves with SELECT new id.
>
> > > On 23 сент, 16:00, Siah <[EMAIL PROTECTED]> wrote:
> > >> Hi,
>
> > >> When in a model you run something like this:
>
> > >>>> obj = Product(name='Apple')
> > >>>> obj.id
>
> > >> 4
>
> > >> I realize the first statement will turn into an insert table. But,
> > >> how
> > >> does django know of its newly assigned primary key(ID).
>
> > >> I have a legacy database whose ID is assigned with an after trigger,
> > >> and am trying to understand how I can integrate them.
>
> > >> Thanks,
> > >> Sia


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to