Re: [SQLObject] Problem with LEFTJOINOn
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Oleg B. <ph...@ph...> - 2007-08-03 15:46:38
|
On Fri, Aug 03, 2007 at 10:15:50AM -0500, Jim Steil wrote: > x = RailCar.select(...) > > The problem is that I don't get any of the fields from the RailStatus > table in the select. The problem is not with the join, but with your understanding of .select(). SQLObject.select() can do simple or complex joins, but it operates on the primary class, RailCar in your case, and only return fields (attributes, columns) of the table/class, not of any table in a join. Let's look why. You have declared the class like this: class RailCar(SQLObject): brand = StringCol() Now SQLObject knows there are two columns in the table - "id" and "brand". When you call RailCar.select() SQLObject returns a list of RailCar instances, and these instances can only contain the declared attributes. There is no room for attributes from different classes. There are two or more "id" columns in a join - what row.id should return? Imagine there are two "name" columns, one in RailCar and the other in RailStatus, what row.name should return for an every row in a join? > What do I need to do to get the fields from both > the joined tables into the result? Any pointers would be appreciated. Declare a relation between these tables/classes: class RailStatus(SQLObject): railCar = ForeignKey("RailCar") While looping over RailCarStatus.select() use "railCar" that points to RailCar table. SQLObject fetches an instance: for status in RailCarStatus.select(): car = status.railCar print car print car.brand Or you can do vice versa: class RailCar(SQLObject): railStatus = ForeignKey("RailStatus") Or you can do both. You can use ForeignKey or SingleJoin for one-to-one relation, MultipleJoin for one-to-many or RelatedJoin for many-to-many. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |