On Mar 29, 2008, at 9:13 AM, Bobby Impollonia wrote:

>
> Hi. I recently switched a sqlalchemy project from using manual mapping
> of classes to ext.declarative.
>
> This is in a turbogears project and I am using their metadata and
> mapper. Before I had:
>
> from turbogears.database import metadata, mapper
> users_table = sqlalchemy.Table('users', metadata, ... )
> class User(object): pass
> mapper(User, users_table)
>
> And now I have:
>
> from turbogears.database import metadata, mapper
> from sqlalchemy.ext.declarative import declarative_base
> Base = declarative_base(metadata=metadata)
> class User(Base):
>    __tablename__ = 'users'
>    __mapper__ = mapper

ah...we actually had not made the __mapper__ argument settable as of  
yet.   When using declarative, __mapper__ is readable as the actual  
mapper that was created for the class.  So here, putting a callable  
that generates a mapper is a reasonable idea, though im not sure if  
we'd want to put that as "__mapper__" or "__mapper_callable__"....i  
like the former but some might say its inconsistent with itself, since  
it "changes" from a callable to an actual mapper.

> Any idea how I can get User.query back and have my classes
> automatically associated with my session again? I really like
> declarative and it would be disappointing to have to go back to manual
> mapping of class just for that.

Assuming that TG is using the straight ScopedSession extended mapper,  
the most straightforward way here is to use the extension provided by  
your configured ScopedSession directly:

class MyClass(base):
     __mapper_args__ = {'extension':myscopedsession.extension}


That extension gives you the same behavior that ScopedSession.mapper  
does, including the whole "save-on-init" behavior that I'm not  
thrilled about.  If you only want the "query" property, there is a  
newer way to do that which is not as intrusive as  
ScopedSession.mapper, which is to use ScopedSession.query_property():

     Base = declarative_base(metadata=metadata)
     Base.query = myscopedsession.query_property()

that way all classes which extend from Base will have a "query"  
attribute available, usable as MyClass.query.filter(...)....

But it still may be convenient to add the functionality you described  
earlier, i.e. to just assign the desired mapper callable.  Actually  
now that im thinking about it it might be more convenient as a kwarg  
to declarative_base, like declarative_base(mapper_factory=mapper).   
what do you think ?




--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to