Thread: [SQLObject] Customizing foreign keys naming style in 0.8 ?
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Luper R. <lup...@gm...> - 2007-03-22 16:22:46
|
Hi, I would like to customize the way ForeignKey are named in my database : class Foo(SQLObject): pass class Bar(SQLObject): fooId = ForeignKey("Foo") if __name__ == "__main__": sqlhub.processConnection = connectionForURI("postgres://localhost/test") Foo.createTable() Bar.createTable() With sqlobject 0.8.1, Bar.fooId becomes bar.foo_id_id (was bar.foo_id in 0.7.3, which is what I want). How can I return to the old naming style ? I tried to use a custom style class, but it does not seem to be involved in the ForeignKey name construction. Thanks, Luper Rouch |
From: Oleg B. <ph...@ph...> - 2007-03-22 16:36:04
|
On Thu, Mar 22, 2007 at 05:22:33PM +0100, Luper Rouch wrote: > I tried to use a custom style class, but it does not seem to be > involved in the ForeignKey name construction. Please show a test program that triggers the problem with a style. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Luper R. <lup...@gm...> - 2007-03-22 19:59:18
|
Oleg Broytmann a écrit : > On Thu, Mar 22, 2007 at 05:22:33PM +0100, Luper Rouch wrote: > >> I tried to use a custom style class, but it does not seem to be >> involved in the ForeignKey name construction. >> > > Please show a test program that triggers the problem with a style. > > Oleg. > Ok I found the answer to my question while trying to explain my problem :-) To customize ForeignKey naming you have to reimplement instanceAttrToIDAttr and instanceIDAttrToAttr in your style class. Here is an example : from sqlobject import * class MyStyle(MixedCaseUnderscoreStyle): def instanceAttrToIDAttr(self, attr): return attr def instanceIDAttrToAttr(self, attr): return attr class Foo(SQLObject): pass class Bar(SQLObject): class sqlmeta: style = MyStyle() fooId = ForeignKey("Foo") if __name__ == "__main__": sqlhub.processConnection = connectionForURI("postgres://localhost/test") Foo.createTable() Bar.createTable() This way Bar.fooId corresponds to bar.foo_id in the database ... Thanks ! Luper Rouch |