Thanks for the quick response. However, the problem I face is not
being able to access the id assigned by database but not being able to
modify the corresponding field in Page instance. To be more clear:

bk_ids = {}
for title in ('Tom Sawyer', 'Huck Finn'):
     book = Book(title=title)
     session.add(book)
     session.flush()
     bk_ids[title] = book.id
session.commit()

for i, content in enumerate(('Once upon a time there was a little
fellow called Tom.', 'His surname was Sawyer.')):
    page = Page(i, content, bk_ids['Tom Sawyer'])
    session.add(page)
session.commit()

When I check the database, book_id field in pages table is not
modified as it supposed (or at least I suppose) to be.

On Nov 9, 3:12 pm, Mike Conley <mconl...@gmail.com> wrote:
> The id is generate by the database engine, not SQLAlchemy, so session.add()
> does nothing to push your object to the database and generate the id. You
> need to execute session.flush() after session.add() to write the book to the
> database and generate the id. After the flush() operation, the book id is
> available to save in your dictionary.
>
> something like this:
>
> bk_ids = {}
> for title in ('Tom Sawyer', 'Huck Finn'):
>     book = Book(title=title)
>     session.add(book)
>     session.flush()
>     bk_ids[title] = book.id
> session.commit()
>
> Without the flush(), the id will be NULL.
--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to