On Wed, 2006-09-20 at 20:27 +0200, Alexandre CONRAD wrote:
> First, I created a "tables" file where I can hold all my SA tables.
> 
> models/my_tables.py:
> 
> =============
> from sqlalchemy import *
> 
> # Clients
> clients_table = Table('clients', metadata,
>                        Column('id_client', Integer, primary_key=True),
>                        Column('name', String(40), nullable=False),
>                       )
> 
> # Sites
> sites_table = Table('sites', metadata,
>                      Column('id_site', Integer, primary_key=True),
>                      Column('id_client', Integer,
>                             ForeignKey('clients.id_client')),
>                      Column('name', String(40), nullable=False),
>                     )
> =============
> 
> Then my python objects stored into a "models" file.
> 
> models/my_models.py:
> 
> =============
> from sqlalchemy import *
> 
> class Client(object):
>      def __init__(self, name):
>          self.name = name
> 
> class Site(object):
>      def __init__(self, name, client_id):
>          self.name = name
>          self.name = client_id
> =============
> 
> 
> Then I'd like to map tables and my python objects together.
> 
> models/__init__.py:
> 
> =============
> from sqlalchemy import *
> 
> from my_tables import clients_table, sites_table
> from my_models import Client, Site
> 
> metadata = DynamicMetaData() # Gotta do something with that somewhere...
> 
> client_mapper = mapper(clients_table, Client)
> site_mapper = mapper(sites_table, Site)
> =============

>From an encapsulation standpoint, I would tend to put tables and classes
together by "subject", e.g.,

--- models/clients.py ---
from sqlalchemy import *  # probably better to restrict this

clients_table = Table('clients', metadata,
     Column('id_client', Integer, primary_key=True),
     Column('name', String(40), nullable=False),
)

class Client(object):
     def __init__(self, name):
         self.name = name

client_mapper = mapper(clients_table, Client)

--- models/sites.py ---
from sqlalchemy import *

sites_table = Table('sites', metadata,
    Column('id_site', Integer, primary_key=True),
    Column('id_client', Integer,
        ForeignKey('clients.id_client')),
    Column('name', String(40), nullable=False),
)

class Site(object):
     def __init__(self, name, client_id):
         self.name = name
         self.name = client_id

site_mapper = mapper(sites_table, Site)

--- models/__init__.py ---
from sqlalchemy import *
from clients import Client
from sites import Site

metadata = DynamicMetaData()
---

The point is that if I add or change a column to the clients table, I'll
probably have to change the Client class.  If one class depends on
another, I could import the latter in the former module.  Granted that
can get tricky with circular dependencies, but those are very rare.  Now
this is all from my C/C++ "upbringing" so I must admit I have little
practical experience with this approach in Python.

> But basicly, that's how I see things best. I don't want everything 
> inside the models/__init__.py file because bigger projects might take up 
> some pages and I don't want to have it all-in-one-file-long-file. It 
> might not be the right way to do it, but I hope it is. :)

The one-big-model-file is an issue that I had raised in an earlier post.
I don't understand the internals of Python and Pylons well enough, but
it seems like a waste of cycles to load a ten-table/class model for say,
a contact page which will only trigger a mail send and probably not even
need to access the database.

I was also discussing with Mike Bayer the issue of not repeating the
database schema.  The autoload=True option allows you to eliminate the
repetition but there is a cost involved:  you have to be connected to
the database (which means you can't use DynamicMetaData) because SA has
to query the db's catalogs (or data dictionary) to do the autoload.
OTOH, I wonder how does Django's ORM and SLQObject (which emphasize DRY)
manage this.  They presumably implement autoload and not give you a
choice.  Even with connection pooling and session and database caches,
it seems to me there's a performance impact, unless there's some
selective mechanism that only queries that which is needed.

I'm still learning so please forgive my ignorance.

Joe


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss
-~----------~----~----~----~------~----~------~--~---

Reply via email to