Thanks very much.
Would I be correct in assuming that in the code example in the
documentation at:
http://www.sqlalchemy.org/docs/05/mappers.html#using-descriptors
the first argument to the mapper should be EmailAddress rather than
MyAddress?
class EmailAddress(object):
def _set_email(self, email):
self._email = email
def _get_email(self):
return self._email
email = property(_get_email, _set_email)
mapper(MyAddress, addresses_table, properties={ <---- ???
'_email': addresses_table.c.email
})
On Jan 21, 11:45 pm, Michael Bayer <[email protected]> wrote:
> On Jan 21, 2009, at 6:15 PM, MHC wrote:
>
>
>
>
>
> > Perhaps someone could offer some guidance here. I am trying to make a
> > column_property. The value of this property is supposed to be the
> > concatenation of two fields from different tables, both joined to the
> > mapped table.
>
> > To make an example I have renamed these tables parent, child, and
> > grandchild. In this application, a parent can have zero or more
> > children. A child can have zero or one grandchildren. If there's a
> > child, one of the parent's children has a column first_child == 1.
>
> > The column property is supposed to be the concatenation of the name of
> > the first child and the name of the child's grandchild, if any. (One
> > of these names will always be NULL, and the other non-NULL). This
> > seems to work if there is a grandchild. But if there isn't a
> > grandchild I seem to be picking up the wrong grandchild. But so far
> > there's only one grandchild record, so maybe it always picks up the
> > wrong grandchild.
>
> > I realize this example sounds pretty strange -- in reality it involves
> > molecular biology and makes sense (I say), but I thought the real
> > names of the tables would be more confusing. If this approach is
> > wrong, is there a better way to concatenate two fields from a child
> > and grandchild of the mapped table? Basically I want each parent to
> > wind up with column set to a name of this "first" subsidiary thing,
> > whether it's the child or the grandchild. If anyone had the patience
> > to read this far, here's the example:
>
> > from_obj = self.parentTable.join(self.childTable,
>
> > onclause=self.parentTable.c.id == self.childTable.c.parent_id)
>
> > from_obj = from_obj.outerjoin(self.grandchildTable,
>
> > onclause=self.childTable.c.grandchild_id == self.grandchildTable.c.id)
>
> > special_name = select([self.childTable.c.name +
> > self.grandchildTable.c.name],
> > from_obj=from_obj,
> > whereclause=and_(self.parentTable.c.id ==
> > self.childTable.c.parent_id,
>
> > self.childTable.c.grandchild_id == self.grandchildTable.c.id,
>
> > self.childTable.c.first_child == 1)).as_scalar().label('special_name')
>
> > mapper(self.Parent, self.parentTable,
> > properties=dict(...
> > special_name=column_property(special_name),
> > ...))
>
> > query = query.filter(Parent.somefield.like('%parent with grandchild
> > %'))
> > parents = list(query)
> > print parents[0].special_name
> > Name of grandchild as expected
>
> > query = query.filter(Parent.somefield.like('%parent with no
> > grandchild%'))
> > parents = list(query)
> > print parents[0].special_name
> > Name of some else's grandchild
>
> one issue might be that you have a comparison to NULL going on, in the
> case where you say childtable.grandchild_id==grandchildtable.id.
> Although I think that would cause no row to be returned.
>
> otherwise, see what the SQL looks like. you always have the option of
> using a python descriptor instead of column_property() if the mapper
> is not managing to render the column_property() as expected within the
> larger query.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---