Re: [sqlalchemy] SQLAlchemy introspection of relationships

14 views
Skip to first unread message

Michael Bayer

unread,
Apr 16, 2013, 8:31:38 PM4/16/13
to sqlal...@googlegroups.com
same answer:

sqlalchemy has no built in facility for generating relationship() structures automatically. there's a library called sqlautocode that does this but I don't know its status. 



On Apr 12, 2013, at 11:03 AM, ken <cmo...@reddeer.gb.com> wrote:

This has also been posted here: http://stackoverflow.com/questions/15950375/sqlalchemy-introspection-of-relationships, shoulda asked here first.

I have a use case for modelling data in a SQL schema rather than through defining SQLAlchemy objects. However, i require a way to construct the corresponding SQLAlchemy objects automatically from the SQL schema.

Currently i have:

from sqlalchemy import MetaData                                                                                                                                                   
from sqlalchemy.ext.declarative import declarative_base                                                                                                                           

meta = MetaData(bind=your_engine)                                                                                                                                                 
meta.reflect()                                                                                                                                                                    

# get the table which we would like to represent as a Python object                                                                                                               
table = meta.tables['your_table_name']                                                                                                                                            

# define the object which we will use as the base class                                                                                                                           
base = declarative_base()                                                                                                                                                         
# create db object                                                                                                                                                                
MyObject = type(str(table.fullname), (base,), {'__table__': table})

however, the relationships between objects are not created. Any ideas how to go about doing this?

Thanks!

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages