5dollarwhitebox.org - theboxownsyou

  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login
Home › Blogs › drks's blog

RSS Feed

Configuring Self-Referential Relationships in TurboGears 2.1

drks — Thu, 2010-07-08 22:13

It has been a while since I felt inclined to put my face through a wall, and endure physical pain rather than be defeated yet another hour (or day) on a Python/TurboGears road block. Well, over the last few days I have been stumped by self-referential relationships in SQLAlchemy. For those not familiar, this is a simple yet common scenario where a Model has a parent or child of itself that is the same Model. For example:

class Package(DeclarativeBase):
    _tablename_ = 'packages'
    id = Column(Integer, primary_key=True)
    label = Column(Unicode(32), nullable=False)
    parent_id = Column(Integer, ForeignKey('packages.id'), nullable=True)
    ...

In this model, I have a Package... that might have a parent package, or it might _be_ a parent and have children. Reading the SQLA documentation didn't get me too much closer to getting this to work, rather following the examples I would constantly get errors similar to:

ArgumentError: Relation Package.parent could not determine any local/remote column 
pairs from remote side argument set([<sqlalchemy.sql.expression.ColumnClause at 
0x102ef75d0; Package.c.id>])

After some googling around I was able to come up with the following solution thanks to a post on StackOverflow:

class Package(DeclarativeBase):
    _tablename_ = 'packages'
    id = Column(Integer, primary_key=True)
    label = Column(Unicode(32), nullable=False)
    parent_id = Column(Integer, ForeignKey('packages.id'), nullable=True)
    parent = relation('Package', remote_side=[id], backref='children')

Note the "parent" relation that I've created. This should allow for a bi-directional relationship between Parent and Child packages. Now I can move on to finishing the last two days of work I've not been able to get to because of this little pest.

Thanks to mpedersen in #turbogears for putting me in the right direction as well.

‹ A Better Way To Write Linux Command Line Applications with Python up Python: API Key [Token] Based Authentication in TurboGears 2.1 [UPDATED] ›
  • drks's blog
  • Printer-friendly version

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Remember that the final exam

Cristophe Longpo (not verified) — Sat, 2012-04-21 19:33

Remember that the final exam pacquiao vs bradley tends to be harder than the prelim (practice exam). Study for the prelims as well, though, because if you get a bad paper on the day of the pacquiao vs bradley tickets real exam, your teachers can appeal if you got a good prelim grade.

  • reply

I totally know what you mean.

Anonymous (not verified) — Tue, 2012-04-17 20:33

I totally know what you mean. Python is a great language but creating codes for it can sometimes be a hassle house painters atlanta

  • reply

web content writing service -

Benson (not verified) — Thu, 2012-04-12 04:44

web content writing service - We offer Search Engine Optimization, Web Promotion, Link Building, Triangular Linking.

  • reply

Do SQLA programming tools lay

ritta_black (not verified) — Wed, 2012-03-21 07:30

Do SQLA programming tools lay on the basis of BYOD enterprise management instruments? Is this the very principle behind companies scoring high levels of success on the market? Are these management tools enough in order to implement some new corporatist solutions? It takes some time and inspired vision to change data into positive outcomes. But after all, judging by what I see on the market, I guess there are mainly the steps to take.

  • reply

turbogears

Prm (not verified) — Sat, 2012-03-10 18:03

This is something new to me. I never heard of turbogears before until i stumbled on this blog.

London bus tours
London bus tour

  • reply

Well this is very interesting

almawilliam (not verified) — Mon, 2012-02-13 01:07

Well this is very interesting indeed. Would love to read a little more of this. Great post. Thanks for the heads-up. This blog was very informative and knowledgeable..!
cell phone listening

  • reply

website design cumbria -

Allenwood (not verified) — Mon, 2012-01-30 16:28

website design cumbria - Search Marketing solutions to UK businesses looking to promote their websites on the internet and through the search engines.

  • reply

TURBOGEARS

pascal (not verified) — Thu, 2012-01-19 04:06

I am completely new to Turbogears. Need to know about this.
Mauritius Photos

  • reply

YOU ARE WELCOME LADYS AND

Anonymous (not verified) — Sun, 2012-01-29 07:20

YOU ARE WELCOME LADYS AND GENTELMENS!!!!!!!!!!!!!!
graco duoglider

  • reply

Thanks for the awesome

katewilliam (not verified) — Mon, 2012-01-09 15:26

Thanks for the awesome article here. I am a huge fan of design so it is really interesting for me to read such stuff. I just hope to see more such nice articles.!
cell spyware

  • reply

I have been searching for

vesper (not verified) — Wed, 2011-12-07 10:14

I have been searching for this quality blogs New Year Greetings regarding this niche. Searching in Yahoo drove me here, I just found this kind of satisfactory readings i was looking for. I must bookmark this website to avoid missing it again.

  • reply

pandora bracelets australia

pandora bracelets australia (not verified) — Thu, 2011-11-24 07:52

pandora bracelets australia Jewellery Group of Germany, adhere towards the definitions of an extraordinary undertaking to create little goods, style for that worth of cash, sabo earrings uk Packaging is nicely recognized for its style equipment pandora bracelets australia consists of the availability of excellence in it, but has an stylish style with coloured rings, instant, daring and stylish in look, and a few other kinds. Like a make a difference of reality the sequence pandora bracelets australia Charms complete 2nd standard style types that will adapt to any scenario. Use a beautiful chance to construct lines of interpretation and design to create a mark within the hearts of women.

  • reply

This script definitely helped

Dave Johanson (not verified) — Fri, 2011-11-18 09:49

This script definitely helped me out, class Package(DeclarativeBase):
_tablename_ = 'packages'
id = Column(Integer, primary_key=True)
label = Column(Unicode(32), nullable=False)
parent_id = Column(Integer, ForeignKey('packages.id'), nullable=True)
parent = relation('Package', remote_side=[id], backref='children') thanks for finding it.

Dave
4G LTE Phones

  • reply

quickly evokes a seductive

Darlene Antley, I (not verified) — Wed, 2011-10-19 13:59

quickly evokes a seductive essence with the Chinese coast.You'll find it plays coordinator to Hermes Birkin Replica Encouraged by as well as infused on this exhilarating smell, the Neroli Portofino number of bath and the entire body items revitalizes in addition to nourishes the skin with nature-rich compounds including olive berry oil

  • reply

Take Care of Your birkin

Anonymous (not verified) — Mon, 2011-09-26 10:42

Take Care of Your birkin bag

birkin bag, whose beauty and elegance wins your love at first sight, whose rich material and exquisite craftsmanship make you hard to put your hands off, is it the same as the new one you bought it? Well, lady, please take it care as your baby. It is not only your investment, but also your good friend. Curing and cherishing your Hermes handbag, it maybe passes down to your future generations, and they can share the Hermes glamour with you!

  • reply

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
Input format
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <diff>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <perl>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options



Who's online

There are currently 0 users and 3 guests online.
  • blog
  • projects
  • articles
  • tech wiki
  • about
  • login

5dollarwhitebox.org is not responsible in anyway for actions performed based on information found on this site.