Not sure about TG2, but I have been using SQLAlchemy 0.5 against TG 
1.0.7 for a while now. Note in my case I'm using SQLAlchemy from the 
command line, and then importing my SA code into model.py in TG and 
exposing it as a webapp. I'm not sure I remember why but adding 
'save_on_init=False' to my mappers fixed something.
 

from sqlalchemy import *
from sqlalchemy.orm import *

# Turbogears compatability 
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual
Session = scoped_session(sessionmaker(autoflush=True, autocommit=True))
mapper = Session.mapper

db_uri='postgres://%s:%...@%s/%s' % (DB_USER,DB_PASS,DB_HOST,DB_NAME)
db = create_engine (db_uri)
metadata = MetaData(db)

class Note(object):
    pass

class User (object):
    pass

note_table = Table('note', metadata, autoload=True)
users_table = Table ('users', metadata, autoload=True)

mapper(Note, note_table, save_on_init=False)

mapper (User, users_table, properties={
        'Notes':relation(Note, backref='Author', lazy=True)},
        save_on_init=False)

........

session=create_session()
user=session.query(User).get('dgardner')
for n in user.Notes:
    print n.note


Artem Marchenko wrote:
> Hi All
>
> I am playing with TurboGears 2 for about a week already. So far
> everything looks great and I am able to create functionality with
> exceptionally small amount of neat-looking code.
>
> At the moment I am stuck with the SQLAlchemy based relations. I want
> to build simple one-to-many relationship (think User-Addresses for
> example) and I can't get how to do that. Wiki20 tutorial doesn't touch
> it and SQLAlchemy tutorial at 
> http://www.sqlalchemy.org/docs/05/ormtutorial.html#building-a-relation
> seems to be based on a functionality not yet supported (not
> recommended?) by TG yet. I was able to find some topics about similar
> things in this group, but it looks like TG and SQLAlchemy evolve
> faster, than tutorials and discussions :)
>
> Could anybody, please, post a simple one to many model example?
> Something that would allow accessing addresses e.g. via the following:
>   
>>>> jack = User('jack')
>>>> jack.addresses
>>>>         
> []
>
>
> Best regards,
> Artem.
> >
>
>   


-- 
David Gardner
Pipeline Tools Programmer, "Sid the Science Kid"
Jim Henson Creature Shop
[email protected]


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