On Apr 15, 2009, at 6:43 PM, thatsanicehatyouh...@mac.com wrote:
> > Hello, > > I have a question about SQLAlchemy (well, I have a lot of questions, > but I'll try to space them out a bit!). I'm very new to it (and > python) but not databases and ORMs. > > I like that I can use reflection to define tables, and I really want > to use that since I don't want to update python classes when I modify > a table. I am defining my table like this: > > metadata = MetaData() > metadata.bind = engine > platesTable = Table('plate', metadata, autoload=True) > > Next, I want to map my plate table to a lookup table: > > surveyTable = Table('survey', metadata) > x = sql.join(platesTable, surveyTable, platesTable.c.survey_pk == > surveyTable.c.pk) > mapper(??plate class??, x, properties={'pk':[platesTable.c.survey_pk, > surveyTable.c.pk]}) > > The problem is that I don't know what to put in for the plate class > since I used reflection and haven't defined one. Does this mean that I > have to define the class by hand and can't have it done automatically? > Can the autoload figure these relationships out? > > Thanks in advance for any help! > the minimal class definition would be : class PlateClass(object): pass if you like, its easy enough to make a mapper function which does something like this: def map_my_table(tablename, metadata): table = Table(tablename, metadata, autoload=True) cls = type.__new__(type, tablename, (object,), {}) mapper(cls, table) return cls usage would be like: my_class = map_my_table('mytable', metadata) x = my_class() next - there is an extension provided with SQLAlchemy called SQLSoup which already does something very similar to this. The documentation is on the site and while the classes it produces have limited capabilities, it does allow you to work with the ORM Query object without needing to define any of your own classes. http://www.sqlalchemy.org/docs/05/reference/ext/sqlsoup.html next again - theres a recipe on the wiki called AutoCode which I believe some people are using to generate classes...hmm apparently its become its own project: http://code.google.com/p/sqlautocode/ finally, I would most recommend the declarative package, which can make usage of autoloaded tables as well. Usage would be like: Base = declarative_base() class MyClass(Base): __table__ = Table('mytable', metadata, autoload=True) but that might be more explicit than you had in mind. I would also note that your map to a join of two tables may be better suited to using joined table inheritance. If it fits your model, SQLAlchemy can make much better decisions knowing its doing joined table inheritance, as opposed to a map to an arbitrary join. docs for that are at http://www.sqlalchemy.org/docs/05/mappers.html?highlight=joined%20table%20inheritance#joined-table-inheritance . --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---