[sqlalchemy] Checking the handling of unique keys/indexes

2014-12-20 Thread SF Markus Elfring
Hello,

I am using source code like the following in one of my scripts
where I am trying the software packages SQLAlchemy 0.9.7-77.1
and SQLite 3.8.7.2-1.1 out on my openSUSE system.

...
engine = create_engine(sqlite:///:memory:, echo = False)
base = declarative_base()

class position(base):
   __tablename__ = positions
   function = Column(String, primary_key = True)
   source_file = Column(String, primary_key = True)
   line = Column(Integer, primary_key = True)
   column = Column(Integer, primary_key = True)
   void = Column(Integer, default = 0)
   static = Column(Integer, default = 0)
   data_type = Column(String)
   parameter = Column(String)
...
def store_positions(fun, type, point, places):
Add source code positions to an internal table.
...



Now I stumble on an error message like the following.
...
cursor.executemany(statement, parameters)
sqlalchemy.exc.IntegrityError: (IntegrityError) UNIQUE constraint failed: 
positions.function, 
...


The message might be appropriate in principle for my concrete use case.
But I observe that the constraint violation is reported a bit
too late because I got the impression from corresponding debug
output that three rows were added to the shown table here
with unique attributes.

I would appreciate your explanations and further advices.

Regards,
Markus

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] ResourceClosedError on Postgres

2014-12-20 Thread alchemy1
I'm getting this error with the following code. Unfortunately none of the 
code in the stacktrace is my code, it's all SQLAlchemy stuff, so I can't 
tell where I'm causing the problem.

Traceback (most recent call last):
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_debugtoolbar-2.2.2-py3.4.egg/pyramid_debugtoolbar/panels/performance.py
, line 57, in resource_timer_handler
result = handler(request)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid-1.5.1-py3.4.egg/pyramid/tweens.py
, line 21, in excview_tween
response = handler(request)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py
, line 92, in tm_tween
reraise(*exc_info)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/compat.py
, line 13, in reraise
raise value
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py
, line 80, in tm_tween
manager.commit()
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_manager.py
, line 111, in commit
return self.get().commit()
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
, line 280, in commit
reraise(t, v, tb)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py
, line 55, in reraise
raise value
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
, line 271, in commit
self._commitResources()
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
, line 417, in _commitResources
reraise(t, v, tb)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py
, line 55, in reraise
raise value
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
, line 394, in _commitResources
rm.tpc_vote(self)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/zope.sqlalchemy-0.7.5-py3.4.egg/zope/sqlalchemy/datamanager.py
, line 103, in tpc_vote
self.tx.commit()
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py
, line 375, in commit
self._assert_active(prepared_ok=True)
  File 
/home/user/.virtualenvs/project/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py
, line 223, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed


Here I try to add a user, and when I flush it if I get an IntegrityError it 
means the user already exists.
In this case I search for another object Action. If I get NoResultFound I 
create a new one and try to insert it.
And it's here that the exception happens. Any idea how I can fix this?



DBSession.add(user)
try:
DBSession.flush()
except sqlalchemy.exc.IntegrityError as e:
# User already exists
DBSession.rollback()
user = DBSession.query(User).filter(User.email == email).one()
try:
action = DBSession.query(Action).filter(Action.name == name, 
Action.param == None).one()
except sqlalchemy.orm.exc.NoResultFound:
action = create_new_action()
DBSession.add(action)

return HTTPFound(location=request.route_url('page'))



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: ResourceClosedError on Postgres

2014-12-20 Thread alchemy1
Also my DBSession is defined like this

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension
()))

And I've enabled pyramid_tm in my development.ini file for Pyramid.


On Sunday, December 21, 2014 1:09:58 AM UTC+1, alchemy1 wrote:

 I'm getting this error with the following code. Unfortunately none of the 
 code in the stacktrace is my code, it's all SQLAlchemy stuff, so I can't 
 tell where I'm causing the problem.

 Traceback (most recent call last):
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_debugtoolbar-2.2.2-py3.4.egg/pyramid_debugtoolbar/panels/performance.py
 , line 57, in resource_timer_handler
 result = handler(request)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid-1.5.1-py3.4.egg/pyramid/tweens.py
 , line 21, in excview_tween
 response = handler(request)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py
 , line 92, in tm_tween
 reraise(*exc_info)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/compat.py
 , line 13, in reraise
 raise value
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py
 , line 80, in tm_tween
 manager.commit()
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_manager.py
 , line 111, in commit
 return self.get().commit()
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
 , line 280, in commit
 reraise(t, v, tb)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py
 , line 55, in reraise
 raise value
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
 , line 271, in commit
 self._commitResources()
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
 , line 417, in _commitResources
 reraise(t, v, tb)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py
 , line 55, in reraise
 raise value
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py
 , line 394, in _commitResources
 rm.tpc_vote(self)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/zope.sqlalchemy-0.7.5-py3.4.egg/zope/sqlalchemy/datamanager.py
 , line 103, in tpc_vote
 self.tx.commit()
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py
 , line 375, in commit
 self._assert_active(prepared_ok=True)
   File 
 /home/user/.virtualenvs/project/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py
 , line 223, in _assert_active
 raise sa_exc.ResourceClosedError(closed_msg)
 sqlalchemy.exc.ResourceClosedError: This transaction is closed


 Here I try to add a user, and when I flush it if I get an IntegrityError it 
 means the user already exists.
 In this case I search for another object Action. If I get NoResultFound I 
 create a new one and try to insert it.
 And it's here that the exception happens. Any idea how I can fix this?



 DBSession.add(user)
 try:
 DBSession.flush()
 except sqlalchemy.exc.IntegrityError as e:
 # User already exists
 DBSession.rollback()
 user = DBSession.query(User).filter(User.email == email).one()
 try:
 action = DBSession.query(Action).filter(Action.name == name, 
 Action.param == None).one()
 except sqlalchemy.orm.exc.NoResultFound:
 action = create_new_action()
 DBSession.add(action)

 return HTTPFound(location=request.route_url('page'))





-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.