On Dec 8, 2008, at 2:18 PM, channing wrote:
>
> I'd like to be able to map read-only attributes based on a join, but
> without mapping the class to a joined table.
>
> ----
>
> Given this structure:
>
> class unit:
> pass
>
> class recsite:
> pass
>
> units = Table('units',metadata,
> Column('unit_id',Integer,
> primary_key=True,nullable=False),
> Column('recsite_id',Integer,
> ForeignKey(recsites.c.recsite_id),
> nullable=False))
>
> recsites = Table('recsites',metadata,
> Column('recsite_id',Integer,
>
> primary_key=True,nullable=False),
> Column('depth',Integer,nullable=False))
>
> mapper(unit,units)
> mapper(recsite,recsites)
>
> ----
>
> Is there a way to map a 'depth' property for unit, without mapping
> unit to units.join(recsites)? I tried adding a ColumnProperty to the
> mapper, but while it succeeds, it returns the wrong value:
>
> mapper(unit,units,
> properties={'depth':ColumnProperty(recsites.c.depth)})
>
> I can't find anything in the documentation for ColumnProperty that
> tells me how to specify the join condition.
you would need to specify a correlated scalar select to
ColumnProperty. The second example in
http://www.sqlalchemy.org/docs/05/mappers.html#sql-expressions-as-mapped-attributes
illustrates this.
An alternative approach would be to build a relation() to a "recsites"
mapper, but to use the association proxy so that you only see the
"depth" attribute. Current docs for that are here
http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html
but there's some errors in the examples on that page; I understand
a new version of that document will be available shortly.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---