[SQLObject] InheritableSQLObject and ForeignKeys: unpredictable behaviour.
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Alan F. <ala...@gm...> - 2005-12-13 15:58:28
|
Hello, I think there's an unpredictable behaviour in InheritableSQLObject and FKs handling. Whenever you define a FK in any 'standard' SQLObject, you get a 'foreignkeyID' attribute returning the same result as getattr(SQLObject,'foreignkey').id but, I think (I didn't check actual debug output), skipping an additional SELECT query from the DB ( I think the 'foreignkeyID' attribute simply returns the value in the column from the table, while the other method actually queries the DB for the record in the referred table and returns the id - same result, longer execution ). Whenever you try doing this with a child InheritableSQLObject, you get an error. Trying to fetch the result from the parent table does no good, since SQLObject returns a child instance anyway, if there's any. I think this is a 'gotcha' - i was widely using such shortcut in my software, and now I'm forced to redesign it :-(. If the 'foreignkeyID' version is 'for internal use only' by the software, it should be prefixed by _, but I think it would be a great improvement to have it working as it is now for InheritableSQLObject as well. #begin inheritablefks.py from sqlobject import * from sqlobject.inheritance import InheritableSQLObject __connection__= #fill in your connection URI class Referred(SQLObject): referred=IntCol() class Standard(SQLObject): standard=IntCol() referred=ForeignKey('Referred') class Parent(InheritableSQLObject): parent=IntCol() referred=ForeignKey('Referred') class Child(Parent): child=IntCol() Referred.createTable() Standard.createTable() Parent.createTable() Child.createTable() ref=Referred(referred=1) std=Standard(standard=2,referred=ref) child=Child(parent=3,child=4,referred=ref) print std.referredID try: print child.referredID except AttributeError: print 'Child:No such attribute' parent=Parent.get(child.id) try: print parent.referredID except AttributeError: print 'Parent: No such attribute' #end inheritablefks.py -- Alan Franzoni <ala...@gm...> - Togli .xyz dalla mia email per contattarmi. To contact me, remove .xyz from my email address. - GPG Key Fingerprint: 5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E |