Seth wrote:
>
> Michael,
>
> Thank you for your quick reply, however I must apologize as I'm a bit
> of a SQLAlchemy newbie.
>
> In the app I'm using the tables are setup using DeclarativeBase, so
> the mapper() function is confusing me with its need for a class to be
> defined as well as a table_name = Table(); and then using table_name
> in the mapper call. Is it possible to use mapper() in a
> DeclarativeBase setup where your columns are defined inside the class?
>
> For reference, here's the Post class:
>
> class Post(DeclarativeBase):
>     __tablename__ = 'posts'
>
>     def __init__(self, id, user_id, body):
>         self.id = id
>         self.user_id = user_id
>         self.body = body
>
>     id = Column(Integer, autoincrement=True, primary_key=True)
>     user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
>     body = Column(UnicodeText, nullable=False)
>
>     user = relation('User', order_by='Post.id', backref='posts')
>     comments = relation('Comments')
>
>
> How would you apply your suggested mapper() call to this type of
> setup? Or do I need to switch things to the "more traditional"
> approach?

there's no restriction when using declarative.  you'd still use
`column_property()` and you can construct SQL expressions using the Column
objects you've just created, i.e.

   comment_count =
column_property(select([func.count(Comment.id)]).where(Comment.post_id==id))

note the usage of "id" which is just the "id" Column object that exists
within the scope of the `User` class declaration.

If its really problematic to have `Comment.id` and `Comment.post_id`
available at that point, you can tack on the property after both classes
are created, which behind the scenes results in an `add_property()` call:

    Post.comment_count =
column_property(select([...]).where(Comment.post_id==Post.id))




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