On 5/20/06, Giorgi Lekishvili <[EMAIL PROTECTED]> wrote:
Hi, I am wondering to learn how to use sqlalchemy mappers with existing (aka legacy) DBs. I'd much appreciate any hint.
I assume you're familiar with the fact that SQLAlchemy can load the table definitions straight from the database (with the autoload=True parameter)? If you didn't know that, then before writing any code, go read http://www.sqlalchemy.org/docs/metadata.myt, followed by http://www.sqlalchemy.org/docs/datamapping.myt; you'll save yourself from writing a lot of unnecessary code that way. If you've already familiarised yourself with those docs and I'm just repeating things you already know, then my best advice is to experiment. Create a "toy" database with a few tables, and then start writing SQLAlchemy code to read the database's table definitions, do some selects, update a few rows, and so on. Then start experimenting with the advanced mapper features (http://www.sqlalchemy.org/docs/adv_datamapping.myt). Do you have large BLOB columns that you only want loaded when you need them, and ignored otherwise? Use deferred column loading (http://www.sqlalchemy.org/docs/adv_datamapping.myt#adv_datamapping_deferred). Do you have some "secondary" tables that you almost always want to load with their "primary" tables? (E.g., you almost always want an employee's address to be loaded at the same time as the record from the Person table)? Then use "eager loading" (http://www.sqlalchemy.org/docs/datamapping.myt#data_relations_eagerload) instead of the default "lazy loading" (which only loads data from the database when it's actually needed). It's kind of hard to give specific advice without knowing the exact details of what you want to do with your legacy DB, but that should give you some pointers to get you started. Basically: immerse yourself in the documentation, and get a good feel for the API by playing around in a "toy" database first. That way you can make all the changes you want: insert new data, update existing data, even drop entire tables. Finally, if all this is far beyond the point where you're at and you really were just looking for a tutorial, I've got one up at http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html. I hope this helps. -- Robin Munn [EMAIL PROTECTED] GPG key 0xD6497014