[SQLObject] sqlobject one-to-many join question
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <jo...@ma...> - 2015-12-15 23:49:35
|
So I've been absolutely loving sqlobject, until I get to actually get to
making relations between tables. At this point, I feel that the
'documentation' covers some points, but really doesn't explain other
bits very well at all.
So I have 2 problems:
The first is that when running my program the first time, it fails to
create all the tables (using .createTable(ifNotExists = True)) and only
several tables are generated, before it errors out with:
> sqlobject.dberrors.OperationalError: Can't create table `rainmeter`.`#sql-65c_209` (errno: 150 "Foreign key constraint is incorrectly formed")
Then running it again causes the rest of the tables to be created.
Here's the relevant parts of my class definitions:
> class Table_Sensors(sqlobject.SQLObject):
> name = sqlobject.StringCol(length=32)
> group = sqlobject.ForeignKey("Table_Groups")
> class Table_Groups(sqlobject.SQLObject):
> name = sqlobject.StringCol(length=32)
> sensors = sqlobject.MultipleJoin("Table_Sensors")
> Table_Sensors.createTable(ifNotExists = True)
> Table_Groups.createTable(ifNotExists = True)
Anyways, this is inconvenient, but it does work after running it again,
so I can put up with it. The real problem I've been having is that I'm
having trouble figuring out how to select all entries in Table_Sensors
that point to a specific Table_Groups.
Here's one example I've tried:
> Table_Groups.select(Table_Groups.q.name=="Feels_Like", Table_Groups.q.sensors==Table_Sensors.q.group, orderBy=Table_Sensors.q.sort_order)
But no matter what I try, even a much simpler query, It just spits back:
> AttributeError: Table_Groups instance has no attribute 'sensors'
As a side-note, I think some of the things I'm trying to do, I could use
j-magic for, but the section on that is incredibly short, and I haven't
been able to get that to work either :/ |