Thread: [SQLObject] MultipleJoin to id
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Oleg B. <ph...@ph...> - 2004-04-22 14:47:06
|
Hello! I need a MultipleJoin, where ForeignKey is the id of the same table. I cannot do it directly, because I cannot redeclare id column, so I implemented it the following way: class Term(SQLObject): translation = IntCol(default=None) # instead of MultipleJoin def _get_translation(self): _translation = self._SO_get_translation() if _translation is None: return None return self.select(self.q.id == _translation, orderBy="code") def _set_translation(self, codeObject): if codeObject is not None: codeObject = codeObject.id self._SO_set_translation(codeObject) _doc_translation = "ID of the parent code term for this code" But this is ugly, and tiresome to reimplement every time I need such a feature. How can I do it in a simpler way? I am using SQLObject 0.5.2 with inheritance patch. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Ian B. <ia...@co...> - 2004-04-22 15:59:55
|
Oleg Broytmann wrote: > Hello! > > I need a MultipleJoin, where ForeignKey is the id of the same table. > I cannot do it directly, because I cannot redeclare id column, so I > implemented it the following way: > > class Term(SQLObject): > translation = IntCol(default=None) # instead of MultipleJoin > > def _get_translation(self): > _translation = self._SO_get_translation() > if _translation is None: > return None > return self.select(self.q.id == _translation, orderBy="code") > > def _set_translation(self, codeObject): > if codeObject is not None: > codeObject = codeObject.id > self._SO_set_translation(codeObject) > > _doc_translation = "ID of the parent code term for this code" > > But this is ugly, and tiresome to reimplement every time I need such > a feature. How can I do it in a simpler way? Hmm... so this doesn't work? class Term(SQLObject): translation = ForeignKey('Term') Hmm... somewhere the terminology is getting all messed up, so I'm not sure how to follow your example. "translation" without your overrides should be the term translated. But then you want a backreference as well -- but you're naming the backreference the same as the forward reference. I think you need another accessor. There might still be a problem after you do that, but it's hard for me to understand this at all right now. Ian |
From: Oleg B. <ph...@ph...> - 2004-04-22 16:12:41
|
On Thu, Apr 22, 2004 at 10:59:25AM -0500, Ian Bicking wrote: > Hmm... so this doesn't work? > > class Term(SQLObject): > translation = ForeignKey('Term') It is not what I want. The table must work (and is working now with my _get_ and _set_) the following way: there is a term and a list of translations. Master term can have or have not translations (aterm.translation is None). If there are translations, they are terms from the same table, and for every translation there is no a subtranslation: for t in aterm.translation: if t.translation is not None: raise TypeError Well, this is not enforced now, and it is not important to my question. The question is - how to make translation MultipleJoin? I'd like to declare class Term(SQLObject): translation = MultipleJoin("Term") but there is no a ForeignKey for it. How can I declare id as the ForeignKey? Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Ian B. <ia...@co...> - 2004-04-22 16:20:01
|
Oleg Broytmann wrote: > On Thu, Apr 22, 2004 at 10:59:25AM -0500, Ian Bicking wrote: > >>Hmm... so this doesn't work? >> >>class Term(SQLObject): >> translation = ForeignKey('Term') > > > It is not what I want. > > The table must work (and is working now with my _get_ and _set_) the > following way: there is a term and a list of translations. Master term > can have or have not translations (aterm.translation is None). If there > are translations, they are terms from the same table, and for every > translation there is no a subtranslation: > > for t in aterm.translation: > if t.translation is not None: > raise TypeError > > Well, this is not enforced now, and it is not important to my > question. The question is - how to make translation MultipleJoin? I'd > like to declare > > class Term(SQLObject): > translation = MultipleJoin("Term") > > but there is no a ForeignKey for it. How can I declare id as the > ForeignKey? Hmm... okay (untested): class Term(SQLObject): translation = ForeignKey('Term') # This is definitely a separate idea from the above!: translatedTerms = MultipleJoin('Term', joinColumn='translation_id') I believe that will work, but I'm not sure. Ian |
From: Oleg B. <ph...@ph...> - 2004-04-22 16:36:50
|
On Thu, Apr 22, 2004 at 11:19:32AM -0500, Ian Bicking wrote: > class Term(SQLObject): > > translation = ForeignKey('Term') > > # This is definitely a separate idea from the above!: > translatedTerms = MultipleJoin('Term', joinColumn='translation_id') > > I believe that will work, but I'm not sure. This should work, and I actually have thought about this solution... That is, if this a subterm, just allow translation to be None, and if it is a mster term, set translation like this? subterm = Term.new() masterterm = Term.new(translation=subterm) print masterterm.translatedTerms # => [subterm] Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |