Tina,

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


iain duncan wrote:
> On Tue, 2007-17-07 at 20:57 +0200, Christopher Arndt wrote:
>> tinab schrieb:
>>> I was hoping that there would be a way to generate the
>>> python model code from an existing MySQL database,
> 
> 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 ), 
> ) )
> 
>  
> 
> 
> > 
> 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to