On Nov 20, 2009, at 17:00, Michael Bayer wrote:

> Lea H wrote:
>> Hi all,
>> I wanted to ask if there is a way to add many to many relationships to
>> the database every time a new object is inserted.
>> I have a Question that has a many to many relationship to AnswerCode.
>> Every time I create and insert a new Question I'd like to add the
>> corresponding answer codes to that question.
>> I tried with the decorators @before_insert and @after_insert, but
>> neither worked. So I wondered if there is a decent way to do that
>> without having to explicitly add the answer codes after creating the
>> question.
> 
> before_insert() and after_insert() are places where you can only affect
> the current row, unless you want to issue INSERT statements of additional
> rows explicitly without the unit of work being involved.   To affect the
> overall unit of work within a flush, you'd use
> SessionExtension.before_flush(), where you can poke around for things that
> are new or dirty and add additional things to the session before the flush
> actually starts.
> 
> However both are more complicated than just implementing this logic within
> Question which I'd argue is the appropriate place for it.   Implement the
> addition of AnswerCode objects to the Question object within the
> constructor (i.e. __init__ method) of Question.

that works perfectly. thank  you. 
I thought that the constructor was called as well if you get a Question from 
the database. Therefore, I didn't put it there. But it seems that this is not 
the case.

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

Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to