python - SQL Alchemy no transaction begun error -
i run following code
db = create_engine('sqlite:///tracking_test.db') db.echo= true metadata = metadata(db) session = create_session(bind=db) #define tables, if exist sqlalchemly find them , sycn otherwise created delivered= table('delivered', metadata, column('delivered_id',integer,primary_key=true), column('domain',string(200)), column('path',string(500)), ) class delivered(object): def __init__(self,obj): self.domain=u'%s' % obj.get("dm","") self.path=u'%s' % obj.get("pth","") report1t = table('clicked', metadata, column('clicked_id',integer,primary_key=true), column('domain',string(200)), column('path',string(500)), ) class report1(object): def __init__(self,obj): self.domain=u'%s' % obj.get("dm","") self.path=u'%s' % obj.get("pth","") metadata.create_all() mapper(report1, report1t) mapper(delivered,delivered) result= {}#some dict newt = delivered(result) if newt: session.add(newt) session.commit() session.flush()
and error sqlalchemy.exc.invalidrequesterror: no transaction begun.
the session dict say:
{'autocommit': true, 'autoflush': false, 'transaction': none, 'hash_key': 4317750544, 'expire_on_commit': false, '_new': {<sqlalchemy.orm.state.instancestate object @ 0x101a79ad0>: <__main__.addelivered object @ 0x101a799d0>}, 'bind': engine(sqlite:///adtracking_test.db), '_deleted': {}, '_flushing': false, 'identity_map': {}, 'dispatch': <sqlalchemy.event.sessioneventsdispatch object @ 0x101a82f90>, '_enable_transaction_accounting': true, '_identity_cls': <class 'sqlalchemy.orm.identity.weakinstancedict'>, 'twophase': false, '_session__binds': {}, '_query_cls': <class 'sqlalchemy.orm.query.query'>}
any 1 know doing wrong?
thanks, cg
it helpful if post full traceback. however, think problem in way create session. think should using sessionmaker
rather create_session
(i've never seen function before, , don't see documented anywhere). sessionmaker
creates new session
class, need instantiate. so:
session = sessionmaker(bind=db) session = session() ... session.add(newt)
Comments
Post a Comment