[sqlalchemy] Re: Session mapper and Class.query() method

2009-07-27 Thread Michael Bayer

naktinis wrote:
>
> I've seen that Session.mapper is deprecated. While I agree that auto-
> adding objects to a session may be a bad idea, I don't see why is it
> so bad to add the query() method directly to the mapped class.
>
> Is there a reason, why I shouldn't do it?

you are free to do it, using a recipe that you tailor to your own needs,
where you are responsible for resolving any undesire side effects.  SQLA
just doesn't want it in core since it is too confusing of a feature to be
part of what's shipped.

Here is the recipe:

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SessionAwareMapper

just add "Session.add(self)" to the __init__ decorator to get the auto-add
thing.




> >
>


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Session mapper and Class.query() method

2009-07-27 Thread King Simon-NFHD78

> -Original Message-
> From: sqlalchemy@googlegroups.com 
> [mailto:sqlalch...@googlegroups.com] On Behalf Of naktinis
> Sent: 27 July 2009 13:07
> To: sqlalchemy
> Subject: [sqlalchemy] Session mapper and Class.query() method
> 
> 
> I've seen that Session.mapper is deprecated. While I agree that auto-
> adding objects to a session may be a bad idea, I don't see why is it
> so bad to add the query() method directly to the mapped class.
> 
> Is there a reason, why I shouldn't do it?
>

The query() method assumes that you are using a scoped session. As long
as you know that you are always going to use a scoped session, you can
use:

  cls.query = scoped_session.query_property()

...to add it back in again. If you ever use explicit sessions, it might
be confusing that sometimes you say "cls.query()", and other times say
"session.query(cls)", but that's up to you. That's the only reason I can
think of for being cautious about cls.query().

Simon

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---