Hi,

If your database is set up to generate auto-incrementing primary keys on
INSERT (such as a MySQL auto-incrementing column), then SA will retrieve
the new value when the object is flushed to the database. When the
object is constructed, the primary key will be None until you flush the
session, at which point the row gets inserted, the new ID gets
generated, read back, and appears on your instance.

(Note that depending on how the database is set up, and how you've told
SQLAlchemy about the column, there may be some subtlety about the
underlying operations that are performed, but that's usually invisible
to the application code)

The SQLAlchemy ORM tutorial is a good introduction to all this:

http://www.sqlalchemy.org/docs/04/ormtutorial.html

In particular, look at the end of the 'Setting up the Mapping', where it
talks about the 'id' attribute being None, and the 'Saving Objects'
session, where it becomes 1.

Hope that helps,

Simon

-----Original Message-----
From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED]
On Behalf Of jdcman
Sent: 04 February 2008 03:04
To: sqlalchemy
Subject: [sqlalchemy] Re: How do I Instantiate a class with an
Auto-assigned Key ID


Ok,   so can you please tell me how I can make a class that
AutoIncrements the ID automatically whenever I need to make a new
record....  let me explain...

As I add records,  I want the ID to Auto Increment.   When you say
"define a constructor for your class which does not require an ID",
but I REQUIRE an ID,  I also require the ID to increment when I add
new record.   You went on to say "SQLALchemy has no constructor
requirements on mapped classes",  what do you mean by that?

Ok,  so can you give me some really simple SQLAlchemy code that would
allow me to increment the ID automatically?    Keep in mind,  that
multiple people may be adding records,  so it has to sort all this
out.

Am I to assume that this is beyond SQLAlchemy,  and no one else would
ever want to do this?   And this was never designed to do this?

I'm a really dumb newbee here,  so please be patient with me...

Thanx

On Feb 3, 7:38 am, "Michael Bayer" <[EMAIL PROTECTED]> wrote:
> jdcman wrote:
>
> > our members table has an ID as theprimarykeywith an auto-assigned
> >keyID.
>
> > When we create the class it is instantiated with the ID and also
> > returns an ID.
>
> > obviously if we provide an ID it conflicts with the auto-generated
ID.
> > and if we don't provide an ID it says we didn't provide enough
> > variables to instantiate the class
> > so how do we define our class so that it can return an ID but
doesn't
> > require one to instantiate the class?
>
> define a constructor for your class which does not require an
id....or,
> don't define a constructor at all.  SQLAlchemy has no constructor
> requirements on mapped classes, that error is being produced by your
own
> application.



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

Reply via email to