Hi!

I'm getting to a point where my model.py gets too large for one file/
module (for my taste).
So I tried to split it up into several modules, but I ran into
problems with the dependencies between the classes/mappings and I
wonder how the other TG users handle this.

A simple example would be a model handling `users` that can
`participate` at an `event`.
I would like to create a module for all user-related stuff (in my case
this would be an extended TG-Identity model) and another one for the
event-related tables/classes/mappings.

Unfortunately, the mapper for the user references a participation from
the event module and the participation table references the user
table.
So I have a cyclic dependency between those modules.
In one single module you can easily resolve this dependency by
defining *all* tables first, then all classes and last, all mappings.
You cannot easily do this when you split up your model into several
modules -- at least if you have the tables, classes and mappings for
one sub-model in one module.
My solution was to defer the creation of the mappings by putting them
in functions that get called after all models have been loaded.
Is there a better/more convenient way of doing this?
How would you organize your model (and your imports, mappings)?

Maybe this is more of a Python problem than a TG/SA problem. I'm
relatively new to all of those ;)
Thank you for any hints on this.

(Maybe sub-optimal) solution to my sample problem:

#== test.py
from common import *
import user, event

user.create_mappings()
event.create_mappings()

#== common.py
from datetime import datetime
from sqlalchemy import MetaData, Table, Column, String, DateTime,
Integer, Unicode, ForeignKey, create_engine
from sqlalchemy.orm import mapper, relation, sessionmaker

engine = create_engine('sqlite:///:memory:')
metadata = MetaData()

__all__ = filter(lambda k: not k.startswith('_'), locals().keys())

#== user.py
from common import *
from event import Participation

user_table = Table('user', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', Unicode(255))
)

class User(object):
    pass

def create_mappings():
    mapper(User, user_table,
        properties=dict(
            participations=relation(Participation, backref='user'),
        )
    )

#== event.py
from common import *

# Event table, class and mapping

event_table = Table('event', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('title', Unicode(255))
)

participation_table = Table('participation', metadata,
    Column('event_id', Integer, ForeignKey('event.event_id'),
primary_key=True),
    Column('user_id', Integer, ForeignKey('user.user_id'),
primary_key=True),
    Column('status', Integer, nullable=False, default=0),
)

class Event(object):
    pass

class Participation(object):
    pass

def create_mappings():
    mapper(Event, event_table,
        properties=dict(
            participations=relation(Participation, backref='event',
cascade='all, delete-orphan'),
        )
    )

    mapper(Participation, participation_table)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to