Martin wrote:

> I've been playing around with pylons + SQLAlchemy for quite some time
> now and things are starting to come together. However, there are some
> aspects that I am still wondering about... and seeing how many
> questions there are about Pylons + SQLAlchemy, it looks like others
> have questions, too. So, could we maybe set up a section in the
> docs/wiki about "SQLAlchemy best practices"?
> 
> Things that I am still unsure about:
> 1) What is the recommended place to define the SQLAlchemy-engine?
> Currently, I have attached the enginge to the g-object... is this good
> or not?
> 
> 2) Where best to initialize the SQLAlchemy session? In the
> models/__init__.py? Somewhere else? Or rather use the threadlocal-mod?
> (Mike Bayer does not recommend the latter, if I understand that other
> discussion thread correctly) Or rather sessioncontext-mod?
> 
> 3) Better to define Tables with bound metadata or unbound metadata?

I'm in the same case, I think there's a real lack about setting up SA 
with Pylons the *explicit* way.

I started to write some "placeholding" code in files, the code does 
*NOT* work, but here is how I like the idea best (cause I like things 
well organized).

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)
=============

This is where I'm not sure how to plug things together. I'd like to 
setup database connection on every request. I followed "Connecting to 
the Database" from QuickWiki 
(http://pylonshq.com/docs/0.9.2/quick_wiki.html#connecting-to-the-database) 
and adapted it the explicit way 
(http://groups.google.com/group/pylons-discuss/browse_thread/thread/35d2e633c534c56c/64ace94f78514ee2).
 
But here we have a problem. When I have my import statments for the 
tables, it's gonna complains about "metadata" as not defined. But I just 
read on the SA site about "Using the global Metadata object" 
(http://www.sqlalchemy.org/docs/metadata.myt#metadata_tables_using). 
This might be the trick for it. But we're for sure less explicit. Gotta 
dig that out. It might be obvious but I'm still new to that.

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. :)

Regards,
-- 
Alexandre CONRAD

--~--~---------~--~----~------------~-------~--~----~
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