On Dec 17, 2011, at 6:26 AM, Manav Goel wrote:

> Hi
>       I created a many to many relation between two tables as shown
> in the tutorial using Association object as I have extra fields in
> connecting table.
> I have table User(user_id), Book(book_id) and UserBook(user_id,
> book_id)
> 
> This code gave me Integrity error of UserBook.user_id of being null.
> Basically it is not user id of user automatically to UserBook object :
> 
> def con(user, mybook):
>     ub = UserBook()
>     ub.book = mybook
>     user.reads.append(ub)
>     session.commit()
> 
> But this code worked :
> def con(user, mybook):
>     ub = UserBook()
>     ub.book = mybook
>     ub.user = user
>     user.reads.append(ub)
>     session.commit()
> 
> Why the first one is not working?

It's not certain without a full example + stack trace, however often this 
occurs because of autoflush (perhaps when user.reads hits the DB to load itself 
first).  The stack trace would illustrate if the integrity error is due to a 
premature autoflush (you'd see _autoflush() in the trace), and there are ways 
to get around that issue.


-- 
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=en.

Reply via email to