Preventing automatic join table creation on "tg-admin sql create"

1 view
Skip to first unread message

Charles Duffy

unread,
Jun 12, 2006, 8:04:10 AM6/12/06
to TurboGears
I have a schema where I'm using RelatedJoin to join through an
intermediate table I've manually built a class for (as it has extra
data about the relationship). However, on running "tg-admin sql
create", the table that's actually created isn't the one I built a
class to map to; rather, it's a minimalist table which contains only
the two endpoints of the RelatedJoin.

I understand that passing createJoinTables=No to the SQLObject
createTable call will cause this to happen. Is there any way to
configure TurboGears such that "tg-admin sql create" will pass in this
parameter?

Jorge Vargas

unread,
Jun 12, 2006, 10:13:44 AM6/12/06
to turbo...@googlegroups.com

you can set up a call on the bottom of your model.py to make that call.
http://www.sqlobject.org/SQLObject.html#creating-and-dropping-tables
although that will eliminate all middle tables



Charles Duffy

unread,
Jun 12, 2006, 10:55:58 PM6/12/06
to TurboGears
Jorge Vargas wrote:
> you can set up a call on the bottom of your model.py to make that call.
> http://www.sqlobject.org/SQLObject.html#creating-and-dropping-tables
> although that will eliminate all middle tables

Thanks for the suggestion!

I actually ended up doing the following at the end of model.py:

classNames = set(locals())
for clsName in list(classNames):
cls = locals()[clsName]
if (isinstance(cls, type) and issubclass(cls, SQLObject)
and cls is not SQLObject
and cls is not InheritableSQLObject):
if hasattr(cls, 'createTableArgs'):
cls.createTable(ifNotExists=True, **cls.createTableArgs)
else:
cls.createTable(ifNotExists=True)
else:
classNames.remove(clsName)


That lets me set createTableArgs for a given class to something like {
'createJoinTables': False } while not changing the defaults more than
need be.

Jorge Vargas

unread,
Jun 12, 2006, 11:02:25 PM6/12/06
to turbo...@googlegroups.com
On 6/12/06, Charles Duffy <duffm...@gmail.com> wrote:

Jorge Vargas wrote:
> you can set up a call on the bottom of your model.py to make that call.
> http://www.sqlobject.org/SQLObject.html#creating-and-dropping-tables
> although that will eliminate all middle tables

Thanks for the suggestion!

:)

I actually ended up doing the following at the end of model.py:

classNames = set(locals())
for clsName in list(classNames):
  cls = locals()[clsName]
  if (isinstance(cls, type) and issubclass(cls, SQLObject)
      and cls is not SQLObject
      and cls is not InheritableSQLObject):
    if hasattr(cls, 'createTableArgs'):
      cls.createTable(ifNotExists=True, **cls.createTableArgs)
    else:
      cls.createTable (ifNotExists=True)
  else:
    classNames.remove(clsName)

great nice little set of code, I luv python :D

That lets me set createTableArgs for a given class to something like {
'createJoinTables': False } while not changing the defaults more than
need be.

that sounds like a nice feature, maybe you should post it to so mailing list, so it could be inserted into sqlmeta.




Alberto Valverde

unread,
Jun 13, 2006, 6:05:49 AM6/13/06
to turbo...@googlegroups.com

You can also try something like:

---- In model_helpers.py -----

class SORelatedJoinNoIT(SORelatedJoin):
"""Base class for a RelatedJoin without automatic intermediate
table.
"""
def hasIntermediateTable(self):
return False

class RelatedJoinNoIT(RelatedJoin):
"""A RelatedJoin without automatic intermediate table.
"""
baseClass = SORelatedJoinNoIT

---- In model.py ----

from sqlobject import *
RelatedJoinOrig = RelatedJoin
from model_helpers import RelatedJoinNoIT as RelatedJoin

Now all your related joins won't generate an intermediate table (of
course, you can avoid the name mangling if you prefer an automatic
intermediate table for most of your joins, then just use
RelatedJoinNoIT for those special joins you provide an intermediate
table for).

The intermediate table auto-generated name concats both tables' names
in alphabetical order, so your "manually" created tables should
follow this convention (ArticleImage, BarFoo, etc...).
Foreign key column names are the name of the joined table + id, all
lowercase: article_id, image_id, etc...

HTH,
Alberto

Reply all
Reply to author
Forward
0 new messages