views:

86

answers:

2

Does anyone has any insight on organizing sqlalchemy based projects? I have many tables and classes with foreign keys, and relations. What is everyone doing in terms of separating classes, tables, and mappers ? I am relatively new to the framework, so any help would be appreciated.

Example:

classA.py # table definition and class A definition
classB.py # table definition and class B definition

### model.py
import classA,classB
map(classA.classA,clasSA.table)
map(classB.classB,clasSB.table)

Including mappers inside classA, and classB works, but poses cross import issues when building relations.. Maybe I am missing something :)

+1  A: 

Take a look at Pylons project including SA setup.

meta.py includes engine and metadata objects

models package includes declerative classes (no mapper needed). Inside that package, structure your classes by relavance into modules.

Maybe a good example would be reddit source code:)

iElectric
@iElectric: You might want to provide an example directory tree showing files in the `models` package.
S.Lott
I checked source code for reddit, but they define most model code in one file.(bidding.py) (for the section that is handled by sqlaclhemy). In general I am having hard time finding examples, beyond simple one file definition.
Sebastian Vantreri
+1  A: 

There are two features in SQLAlchemy design to avoid cross imports when defining relations:

  1. backref argument of relation() allows you to define backward relation.
  2. Using strings (model class and their fields names). Unfortunately this works for declarative only, which is not your case.

See this chapter in tutorial for more information.

Denis Otkidach
Thank you. I took the time today to rewrite my code today to use declarative style. It was either that, or define all my mappings after all the the classes have been loaded. (which was ugly).
Sebastian Vantreri