Nickle wrote:
> I have the following python classes.
>
> class Instrument (object):
>
>     def __init__ (self, name):
>         self.name = name
>
> class Equity (Instrument):
>
>     def __init__ (self, name, currency_id):
>         Instrument.__init__(self, name)
>         self.currency_id = currency_id
>
> class Currency (Instrument):
>     pass
>
> Pretty simple cut down example. Equity has a relationship with a
> currency
>
> Mapping is as follows
>
> instruments = Table ('instruments', metadata,
>     Column ('id', Integer, primary_key=True),
>     Column ('type', String (10), nullable=False),
>     Column ('name', String(50), nullable=False, unique=True),
>     Column ('currency_id', Integer, ForeignKey ('instruments.id'))
>     )
>
> instrument_mapper       = mapper (Instrument, instruments,
> polymorphic_on=instruments.c.type, polymorphic_identity='instrument')
> equity_mapper           = mapper (Equity,
> inherits=instrument_mapper, polymorphic_identity='equity')
> currency_mapper         = mapper (Currency,
> inherits=instrument_mapper, polymorphic_identity='currency',
>                                   properties={'equities': relationship
> (Instrument, backref=backref('currency',
> remote_side=[instruments.c.id]))}
>                                   )
>
>
> One thing I'm not sure about. The currency mapper should have a
> relationship with Currency and not with Instruments. ie. I want to
> restrict the relationship to Currencies.
>
> Is there a standard way of going around this?
>   

I'm assuming that you want Currency.equities to be a relationship to
Equity, not Currency. You should just be able to use Equity instead of
Instrument as the first argument to relationship(). This will cause
SQLAlchemy to include a clause similar to "instruments.type IN
('equity')" in your query when you load Currency.equities.

-Conor

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