I have a relatively complex structure to my data. In essence it's something
like:
* A Foo is a small object containing some data.
* A Bar has a fixed number of (say, 3) references to Foos
* A Baz has a single Bar, plus one additional Foo
* A Qux has a single Foo
I know it seems like an odd structure, but there is other data present at each
level, and you're just going to have to take my word for it that it does make
sense! :P
Here are the SQLA declarative classes representing the above:
class Foo(Base):
__tablename__ = 'foos'
id = Column(Integer, primary_key = True)
name = Column(String)
class Bar(Base):
__tablename__ = 'bars'
id = Column(Integer, primary_key = True)
primary_foo_id = Column(Integer, ForeignKey('foos.id'))
secondary_foo_id = Column(Integer, ForeignKey('foos.id'))
tertiary_foo_id = Column(Integer, ForeignKey('foos.id'))
primary_foo = relationship(Foo, primaryjoin = (primary_foo_id ==
Foo.id))
secondary_foo = relationship(Foo, primaryjoin = (secondary_foo_id ==
Foo.id))
tertiary_foo = relationship(Foo, primaryjoin = (tertiary_foo_id ==
Foo.id))
class Baz(Base):
__tablename__ = 'quxs'
id = Column(Integer, primary_key = True)
bar_id = Column(Integer, ForeignKey('bars.id'))
extra_foo_id = Column(Integer, ForeignKey('foos.id'))
bar = relationship(Bar)
extra_foo = relationship(Foo)
class Qux(Base):
__tablename__ = quxs
id = Column(Integer, primary_key = True)
foo_id = Column(Integer, ForeignKey('foos.id'))
foo = relationship(Foo)
I should note that there is no rule that says a Bar cannot use the same Foo
twice, e.g. as both its secondary and tertiary Foos. Likewise, a Baz's extra
Foo could be the same as one of the Foos of its Bar.
Now, to my actual problem. Given a Foo object, I want an easy way to gather all
references to it. So I want a list of every Bar, Baz and Qux that references
the Foo object, and if a Bar references the Foo two or three times, it should
appear two or three times in the list.
So far the only solution I've come up with is to put a uniquely named backref
into each of the above relationships (they would be somethinig like
'bars_as_primary', 'bars_as_secondary', 'bars_as_tertiary', 'bazs', and
'quxs'), and then add a method, Foo.GetUsages(), which chains all of these
arrays together and returns the result.
That seems a bit verbose, but then it's not exactly a textbook problem. There's
no magical function that returns a list of all of the objects that have a
reference to some specific other object is there?
If you've read this far, thanks!
Cheers,
Cam
Cameron Jackson
Engineering Intern
Air Operations
Thales Australia
Thales Australia Centre, WTC Northbank Wharf, Concourse Level,
Siddeley Street, Melbourne, VIC 3005, Australia
Tel: +61 3 8630 4591
[email protected]<mailto:[email protected]> |
www.thalesgroup.com.au<http://www.thalesgroup.com.au>
-------------------------------------------------------------------------
DISCLAIMER: This e-mail transmission and any documents, files and
previous e-mail messages attached to it are private and confidential.
They may contain proprietary or copyright material or information that
is subject to legal professional privilege. They are for the use of
the intended recipient only. Any unauthorised viewing, use, disclosure,
copying, alteration, storage or distribution of, or reliance on, this
message is strictly prohibited. No part may be reproduced, adapted or
transmitted without the written permission of the owner. If you have
received this transmission in error, or are not an authorised recipient,
please immediately notify the sender by return email, delete this
message and all copies from your e-mail system, and destroy any printed
copies. Receipt by anyone other than the intended recipient should not
be deemed a waiver of any privilege or protection. Thales Australia
does not warrant or represent that this e-mail or any documents, files
and previous e-mail messages attached are error or virus free.
-------------------------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.