Michael Bayer wrote:
class Allocation(Base):
   __tablename__ = 'data'
   id = Column(Integer, primary_key=True)
   trade_id = Column(Integer, index=True)
   instrument_id = Column(ForeignKey(Instrument.id))
   instrument = relationship(Instrument)
   quantity = Column(Integer)
   price_id = Column(ForeignKey(Price.id))
   price = relationship(Price)
I'm not quote sure how to map this, although I guess something along the lines 
of 
http://www.sqlalchemy.org/docs/mappers.html#mapping-a-class-against-arbitrary-selects
would be in order?

maybe.  I'd probably use a view, actually, but mapping to a select is the same 
idea.

Good point. Do SQLite and/or MySQL do views?

However, what that example doesn't show, and what I'm keen to make work, is if 
someone changes attributes of the mapped Trade, what I'd like to have happen 
depends on the attribute:

id,instrument_id, - all underlying rows are updated
quantity,average_price - an error is raised

Are there any examples of this?
Any recommendations? ;-)

right. that pattern seems a little crazy to me

It just might be, in which case only the "base rows" in the data table will be writeable...

this model doesn't give you the best query capability.

Actually the opposite, the above is a little simplified.
The data table also has columns for things like fill_id, account_id, fund_id, strategry_id. These would all be similar to trade_id.

This is deliberately de-normalized such that it becomes trivial to answer queries such as:

"what position does fund x hold in instrument y"
"what position does account a hold in instrument y"
"what is he average price of the trade made up by fills with fill_id x"

These all just become group-by and sum as opposed to heinous joins ;-)
It also means that slicing and dicing by another (as yet unknown) criteria becomes a case of altering the table to add a new column defaulting to None, rather than re-architecting the whole model.

So, definitely interested in modeling things this way, even if only o prove why it's a bad idea ;-)

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to