Tjaart de Beer wrote:
> Hi
>
> I have scoured the web in search of an answer. Is there a way to create
> your model.py file by reverse engineering an existing database? If
> possible the code must be usable by SQLAlchemy.
>
Sure, see the docs in sqlalchemy and look for "autoload"
It works really well, both in mysql and postgres.
for name, in engine.execute('SELECT tablename FROM pg_tables').fetchall():
if not name.startswith('pg_') and not name.startswith('sql_'):
tbl[name] = Table(name, metadata, autoload=True)
I also find this base class quite useful:
class DomainObject(object):
@property
def pk(self):
identity = map(unicode,self.mapper.identity(self))
return urllib.quote('\0'.join(identity))
@classmethod
def from_pk(cls, pk):
keys = urllib.unquote(pk).split('\0')
return session.query(cls).get(*keys)
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__,
','.join(map(unicode,self.mapper.identity(self))))
If your mapper classes inherit from DomainObject, you have an url-quoted
string containing the (possibly composed) primary key by accessing the
"pk" attribute, and can re-create the instance from the pk using
MyClass.from_pk(pk)
I don't know with mysql, but postgres with psycopg2 is happy to convert
the unicode piece to numbers inside .get(*keys). Maybe mysql converts
too much, so it should work too.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---