Thread: [SQLObject] One way relationships
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Gonzalo <go...@da...> - 2005-03-08 01:18:48
|
Hi, I've just started looking into SQLObject and i'm finding this great, being used to plain SQL queries. Here's my question: I have a "type" class, and then a "boat" class. Every boat belongs to a class, so we have a one-to-many relationship: class Boat(SQLObject): ... type =3D ForeignKey('Type') class Type(SQLObject): ... boats =3D MultipleJoin('Boat') Everything is fine so far. My problem is i want to reference a "standard" boat for every "Type" instance: an entry in the Boats table with all the standard specs for a given boat type. I don't need to be able to tell from the Boat object whether this particular boat is the standard boat for a Type.=20 I want aBoat.type.DefaultBoat to be a Boat object, but i want this to be a one-way relation: the only way to tell whether this particular boat is the standard for its type should be to check if aBoat and aBoat.type.DefaultBoat are the same object. Any suggestions on this? Thanks in advance, --=20 Gonzalo Sainz-Tr=E1paga (GomoX) GnuPG Fingerprint: A0AF 3BBF EB93 7EFE 6628 C5A5 F073 9442 6DE4 A497 Public Key: http://pgp.mit.edu:11371/pks/lookup?op=3Dget&search=3D0x6DE4A49= 7 |
From: Sam N. <sa...@se...> - 2005-03-08 04:49:43
|
Gonzalo Sainz-Tr=E1paga wrote: > Hi, >=20 > I've just started looking into SQLObject and i'm finding this great, > being used to plain SQL queries. > Here's my question: > I have a "type" class, and then a "boat" class. > Every boat belongs to a class, so we have a one-to-many relationship: >=20 > class Boat(SQLObject): > ... > type =3D ForeignKey('Type') >=20 > class Type(SQLObject): > ... > boats =3D MultipleJoin('Boat') >=20 > Everything is fine so far. My problem is i want to reference a > "standard" boat for every "Type" instance: an entry in the Boats table > with all the standard specs for a given boat type. > I don't need to be able to tell from the Boat object whether this > particular boat is the standard boat for a Type.=20 > I want aBoat.type.DefaultBoat to be a Boat object, but i want this to b= e > a one-way relation: the only way to tell whether this particular boat i= s > the standard for its type should be to check if aBoat and > aBoat.type.DefaultBoat are the same object. >=20 > Any suggestions on this? >=20 > Thanks in advance, Try this: class Type(SQLObject): ... boats =3D MultipleJoin('Boat') standardBoat =3D ForeignKey('Boat') I use this type of data definition a lot (for things like primaryAddress=20 etc.). Hope that helps! - Sam |
From: Gonzalo <go...@da...> - 2005-03-08 06:41:06
|
On Mon, 2005-03-07 at 20:49 -0800, Sam Nilsson wrote: > Try this: >=20 > class Type(SQLObject): > ... > boats =3D MultipleJoin('Boat') > standardBoat =3D ForeignKey('Boat') That's working very well. I'd only like to point the fact that most ForeignKey rows in cases like this need to have "default=3DNone" passed. Thanks, --=20 Gonzalo Sainz-Tr=E1paga (GomoX) GnuPG Fingerprint: A0AF 3BBF EB93 7EFE 6628 C5A5 F073 9442 6DE4 A497 Public Key: http://pgp.mit.edu:11371/pks/lookup?op=3Dget&search=3D0x6DE4A49= 7 |
From: Frank B. <fb...@fo...> - 2005-03-08 16:50:16
|
Hallo, Gonzalo Sainz-Trápaga hat gesagt: // Gonzalo Sainz-Trápaga wrote: > On Mon, 2005-03-07 at 20:49 -0800, Sam Nilsson wrote: > > Try this: > > > > class Type(SQLObject): > > ... > > boats = MultipleJoin('Boat') > > standardBoat = ForeignKey('Boat') > > That's working very well. I'd only like to point the fact that most > ForeignKey rows in cases like this need to have "default=None" passed. Ahem: How do I do this? ForeignKey('Boat', default=None)? Ciao -- Frank Barknecht _ ______footils.org__ _ __latest track: fqdn _ http://footils.org/cms/show/38 |
From: Sam N. <sa...@se...> - 2005-03-08 20:23:04
|
Frank Barknecht wrote: > Hallo, > Gonzalo Sainz-Tr=E1paga hat gesagt: // Gonzalo Sainz-Tr=E1paga wrote: >=20 >=20 >>On Mon, 2005-03-07 at 20:49 -0800, Sam Nilsson wrote: >> >>>Try this: >>> >>>class Type(SQLObject): >>> ... >>> boats =3D MultipleJoin('Boat') >>> standardBoat =3D ForeignKey('Boat') >> >>That's working very well. I'd only like to point the fact that most >>ForeignKey rows in cases like this need to have "default=3DNone" passed= . >=20 >=20 > Ahem: How do I do this? ForeignKey('Boat', default=3DNone)? >=20 > Ciao Yes! I thought about including the default=3DNone in my example, but I=20 didn't want to add another dimension to the question. Now I see that I=20 should have. standardBoat =3D ForeignKey('Boat', default=3DNone) should work fine. it will use NULL (in mysql at least) for a db value.=20 then you can test for a standard boat like: if type.standardBoat: yada yada yada - Sam |