Hi Michael,

since i might also start with a Role instance and add User instances to
them i need a symmetrical solution with regard ref and backref (and i
think  casade_backrefs=False
would not work in that case?).

So i tested two solutions which both seem to work in this specific case:

 - create a separate session for the query on Role
(session.query(Role).all()); not the same session the User instance is in
(as i did before)
 - set autoflush to false on the query as in
(session.query(Role).autoflush(False).all())

Since you did not mention the second solution, i wondered whether there are
possible disadvantages to the second approach; the first one seems slower
but cleaner.

Cheers, Lars


On Wed, Nov 20, 2013 at 4:51 PM, Michael Bayer <mike...@zzzcomputing.com>wrote:

> you either want to prevent User from being cascaded and/or added into the
> Session ahead of time, which if this is occurring via backref you might
> want to look at casade_backrefs=False (
> http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html?highlight=cascade_backrefs#controlling-cascade-on-backrefs).
>      If not that, then use “with session.no_autoflush” for the period of
> time that you query for Role.
>
>
>
>
> On Nov 20, 2013, at 8:02 AM, lars van gemerden <l...@rational-it.com>
> wrote:
>
> Hi all,
>
> I have 2 sqla classes with mapped tables: User and Role with a many to
> many relationship (User.roles/Role.users) through an association table.
> User has columns email and password, both non nullable.
>
> I create an instance of User but do not commit it yet, next i do a query
> on Role: session.query(Role).all() (to be able to let an end-user choose
> one or more roles for the for the newly created User).
>
> This gives the error: IntegrityError: (IntegrityError) User.email may not
> be NULL u'INSERT INTO "User" (_created_, discriminator, email, password)
> VALUES (CURRENT_TIMESTAMP, ?, ?, ?)' (None, None, None)
>
> I think the query tries to flush the instance of User, which causes the
> error.
>
> My questions is: Why and how do i avoid it? I just want a list of all
> roles currently in the Role table to be able to assign one or moe to the
> User instance.
>
> Cheers, Lars
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
>
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


-- 
====================================
Lars van Gemerden
l...@rational-it.com
+31 6 26 88 55 39
====================================

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to