Thank you.
Tina Matter
--
-------------------------------------------------------
*Tina Matter*
Web Software Developer
Department of Mechanical Engineering
University of Michigan
734-763-3184
-------------------------------------------------------
If your using SQLObject, then check this page:
http://docs.turbogears.org/1.0/GettingStarted/UseDatabase under Define
Data Models.
I haven't learned SQLAlchemy yet (next thing to do).
HTH,
Jeff
Tina
Have you read this?
http://www.sqlalchemy.org/docs/metadata.html#metadata_tables_reflecting
Chris
I just did this with SQLalchemy and it was surprisingly easy. You will
probably want to go straight to the SA docs. All you need to do is make
a stub class and then use assign_mapper to map your stub class to the
existing table. You will probably also need to explicitly state the
relationship keys, which can be found in the SA docs in the advanced
data mapping section.
Here is an example from my project, please excuse the poor formatting:
# to load the table definition by introspecting the database
heard_table = Table('how_heard', metadata, autoload=True)
class Heard(object):
pass
assign_mapper(session.context, Heard, heard_table )
champions_table = Table('users', metadata, autoload=True)
class Champions(object):
pass
assign_mapper(session.context, Champions, champions_table )
# to load the table definition by introspecting the database
orders_table = Table('orders', metadata, autoload=True)
class Orders(object):
pass
assign_mapper(session.context, Orders, orders_table )
locations_table = Table('locations', metadata, autoload=True)
class Locations(object):
# attribute for total number of boxes ordered by a location
def _box_total(self):
count = 0
for order in self.orders:
if order.status == 'verified': count += order.quantity
return count
box_total = property( _box_total )
locations_dates_table = Table('locations_dates', metadata,
autoload=True )
class LocationDates(object):
pass
# LocationDates mapper
assign_mapper(session.context, LocationDates, locations_dates_table,
primary_key= [ locations_dates_table.c.location_id,
locations_dates_table.c.date ],
properties=dict(
locations=relation( Locations,
primaryjoin=and_( locations_table.c.location_id==locations_dates_table.c.location_id ), foreignkey=locations_dates_table.c.location_id ),
)
)
# because the old tables don't have foreign key defs, we need to
explicitly state them here
assign_mapper(session.context, Locations, locations_table,
properties=dict(
orders=relation(Orders,
primaryjoin=and_( orders_table.c.location_id==locations_table.c.location_id ), foreignkey=orders_table.c.location_id ),
champion=relation( Champions,
primaryjoin=and_( locations_table.c.user_id==champions_table.c.user_id ), foreignkey=locations_table.c.user_id ),
dates=relation( LocationDates,
primaryjoin=and_( locations_table.c.location_id==locations_dates_table.c.location_id ),
foreignkey=locations_dates_table.c.location_id,
order_by=locations_dates_table.c.date ),
) )
And the code here may help too:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/AutoCode
and the addon AutoCode2 is tested with MySQL, it appears.
Paul
Tina
Thank you.
Tina