Hi

I was using SQLObject for my project, but now I have started to shift
the code using SQLAlchemy. I am using TurboGears-1.0.4.4 and
SQLAlchemy-0.4.5.

In the model.py I am creating a global session using sessionmaker. In
all the other files I am creating session object. The brief code in
model.py with a table User and another file Action.py is given below.
In Action.py I am using session to query the User table. If the query
results in an exception, I am creating a User object. When I try that,
it returns an error:

(<class 'sqlalchemy.exceptions.InvalidRequestError'>,
InvalidRequestError("Object '[EMAIL PROTECTED]' is already attached to
session '41500432' (this is '41500304')",), <traceback object at
0x0279E558>)

model.py
=======
from datetime import datetime
import pkg_resources
pkg_resources.require("SQLAlchemy>=0.3.10")
from turbogears.database import metadata, mapper
from sqlalchemy import Table, Column, ForeignKey,
ForeignKeyConstraint,String, Unicode, Integer, DateTime
from sqlalchemy.orm import relation

import turbogears.config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

user_table =  Table('user_table', metadata,
    Column(u'id', Integer, primary_key=1),
            Column(u'username', String(length=150,
convert_unicode=False, assert_unicode=None), primary_key=0,
nullable=False),
            Column(u'fullname', String(length=150,
convert_unicode=False, assert_unicode=None), primary_key=0),
            Column(u'primary_email_id', String(length=150,
convert_unicode=False, assert_unicode=None), primary_key=0),
            Column(u'password', String(length=150,
convert_unicode=False, assert_unicode=None), primary_key=0),
            Column(u'level', String(length=150, convert_unicode=False,
assert_unicode=None), primary_key=0),
    )

class User(object):
        def __init__(self,username,fullname, primary_email_id, password,
level):
                self.username =username
                self.fullname = fullname
                self.primary_email_id = primary_email_id
                self.password = password
                self.level = level

mapper(User, user_table)

url= turbogears.config.get('sqlalchemy.dburi')
engine = create_engine(url, echo=True)

Session = sessionmaker(bind=engine, autoflush=True,
transactional=False)

end of model.py
=========

Action.py
=======
import mailvault.model
from mailvault.model import Session

def foo():
        session = Session()
        try:
                result =
session.query(mailvault.model.User).filter_by(username='admin').one()
                session.close()
        except:
                session.clear()
                session.close()

                session = Session()
                try:
                        session.save(mailvault.model.User(
                                username="admin",
fullname="Administrator",
                                primary_email_id="",
password=password, level="1"))
                        session.commit()
                        session.close()
                except:
Thanks
Roopesh
--~--~---------~--~----~------------~-------~--~----~
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