Re: [sqlalchemy] Re: InvalidRequestError

2011-03-15 Thread Michael Bayer

On Mar 14, 2011, at 6:14 PM, Ajay wrote:

 Thanks Mike for your quick response.
 
 I removed the zope.sqlalchemy extension and I am no longer getting the
 zope error.
 
 Since we are using a scoped_session, we don't think it's a threading
 issue although we are running in a multi-threaded env.
 
 I am trying to create this exception to occur in my test env.
 
 What state do we have to leave the Session object in - inorder to
 generate an 'InvalidRequestError' exception.
 Again, assuming that it is not a multithreaded concurrency issue given
 I am using sessions produced from ScopedSession.
 
 My assumption is that since I am using ScopedSession, each thread gets
 it's own session object and session objects are not shared among
 threads.
 
 Again appreciate your help with this.

the issue is just when an error occurs during flush, which results in a 
rollback of the transaction - the Session is then left in a state where it 
requires that you call rollback().   Any operations with the session before 
rollback is called will emit this exception.The condition is not inherently 
related to threading, but can be a side effect of concurrent access to a single 
Session.






 
 thanks!
 
 Ajay
 
 
 
 
 On Mar 12, 11:38 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Mar 11, 2011, at 7:51 PM, AJAY PATTNI wrote:
 
 
 
 We use sqlalchemy 0.4.6 with Elixir 0.5.2 and zope.sqlalchemy
 
 Every once a while we get this error(see below):
 In a previous thread somebody said, just 'rollback your session when
 the exception is raised'?
 
 We use a global session as follows:
 
 __session__ =
 scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True,extension=ZopeTransactionExtension()))
 
 All the rest of the transactional machinery is handled by Elixiry
 Entity object.
 
 So my question - how can we rollback in this situation?
 
 We tried to do __session__.remove() which seems to work but then zope
 seems to have a problem after it completes the request.
 
 A description of what this error means from the perspective of SQLAlchemy 
 only, not that of zope.sqlalchemy for which you should consult their mailing 
 list, is here:
 

 http://www.sqlalchemy.org/trac/wiki/FAQ#Thetransactionisinactivedueto...
 
 However, note that version 0.4.6 of SQLAlchemy is extremely old, and the 
 mechanics of session and transaction have been largely reworked since then.  
  The basic idea that a rollback needs to occur is consistent in that 
 version, however.
 
 Its very likely that some artifact of zope.sqlalchemy is involved in the 
 production of your error here.
 
 ==
 2011-03-11T10:41:22 ERROR Zope.SiteErrorLog
 http://dragon.initiatesystems.com:8080/ati/txm_inject_orm
 Traceback (innermost last):
  Module ZPublisher.Publish, line 121, in publish
  Module Zope2.App.startup, line 238, in commit
  Module transaction._manager, line 89, in commit
  Module transaction._transaction, line 329, in commit
  Module transaction._transaction, line 446, in _commitResources
  Module zope.sqlalchemy.datamanager, line 76, in tpc_vote
  Module sqlalchemy.orm.session, line 263, in commit
  Module sqlalchemy.orm.session, line 176, in _assert_is_open
 InvalidRequestError: The transaction is closed
 =
 
 Appreciate any help I can get on this.
 
 
 Mar 4 12:56:23 pbrk4.den05.accenx.com [err]
 https://acm.accenx.com/mapsrvcluster/ati/acm/tdm/group/19150/broker/2...
 (innermost last):#012 Module ZPublisher.Publish, line 115, in
 publish#012 Module ZPublisher.mapply, line 88, in mapply#012 Module
 ZPublisher.Publish, line 41, in call_object#012 Module
 Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
 Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
 Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
 None, line 90, in processMsg#012 - PythonScript at /ati/acm/tdm/group/
 processMsg used for /ati/acm/tdm/group/19150/broker/25150/site/
 705077435#012 - Line 90#012 Module Shared.DC.Scripts.Bindings, line
 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
 _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
 in _exec#012 Module None, line 131, in translate#012 - PythonScript
 at /ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
 translate#012 - Line 131#012 Module Shared.DC.Scripts.Bindings, line
 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
 _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
 in _exec#012 Module None, line 99, in callTxm#012 - PythonScript at /
 ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
 callTxm#012 - Line 99#012 Module Shared.DC.Scripts.Bindings, line
 313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
 _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
 in _exec#012 Module None, line 44, in txm_ampiWrapperOrm#012 -
 PythonScript at 

[sqlalchemy] In-memory object duplication

2011-03-15 Thread jln
Hi

I'm not sure how exactly to describe my problem, or what parts of code
to submit in support of this query. I have spent time Googling but,
since I'm not sure how to define the problem, I'm not sure what to
look for either. Sorry.

I have a feeling that my problem, somehow, relates to sessions, scoped
sessions, transactions, flushes, refreshes, etc - somewhere in that
bundle. And I'm no expert in that department, basically using
SQLAlchemy with whatever automatic settings it comes with, and
flushing objects after fiddling with them. Oh, I'm using Elixir too.

The actual database entries work perfectly though, without any
persistence problems at all. My problem lies squarely with the objects
in memory. After creating a child object, the parent's
InstrumentedList always shows/reports two of the new child object. As
I said, this situation is not persisted to the database at all.

Perhaps a more to-the-point example snippet. I have Document objects
that each have one or more DocumentStatus objects. So, in Document,
the relation is defined as follows:

statuses = OneToMany('DocumentStatus', inverse='doc', cascade='all,
delete-orphan', order_by=['timestamp'])

So, when I create a new DocumentStatus object, Document.statuses lists
two of them, but not actually persisted to the database. In other
words, leaving my Python shell, and starting the model from scratch,
there actually is only one child object (corroborated by squizzing the
database directly). Here's my DocumentStatus.create() class method:

@classmethod
@logged_in
@log_input
def create(cls, doc, status, person=None, date=None):
person=validate_person(person)
if person:
status = DocumentStatus(doc=doc, status=status,
person=person, date=resolve_datetime(date))
if status:
doc.statuses.append(status)
doc.flush()
out = 'Document status created'
success = True
else:
out = 'Document status not created'
success = False
else:
out = 'Person does not exist'
success = False
log_output(out)
return success

I simply don't know why this is happening or, as I said, how to
search, intelligently, for an answer.

Help, or guidance as to what/where to search for a solution, would be
greatly appreciated.

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



RE: [sqlalchemy] In-memory object duplication

2011-03-15 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of jln
 Sent: 15 March 2011 16:37
 To: sqlalchemy
 Subject: [sqlalchemy] In-memory object duplication
 

[SNIP]

 statuses = OneToMany('DocumentStatus', inverse='doc', cascade='all,
 delete-orphan', order_by=['timestamp'])
 
 So, when I create a new DocumentStatus object, Document.statuses
 lists
 two of them, but not actually persisted to the database. In other
 words, leaving my Python shell, and starting the model from scratch,
 there actually is only one child object (corroborated by squizzing
 the
 database directly). Here's my DocumentStatus.create() class method:
 
 @classmethod
 @logged_in
 @log_input
 def create(cls, doc, status, person=None, date=None):
 person=validate_person(person)
 if person:
 status = DocumentStatus(doc=doc, status=status,
 person=person, date=resolve_datetime(date))
 if status:
 doc.statuses.append(status)
 doc.flush()
 out = 'Document status created'
 success = True
 else:
 out = 'Document status not created'
 success = False
 else:
 out = 'Person does not exist'
 success = False
 log_output(out)
 return success
 
 I simply don't know why this is happening or, as I said, how to
 search, intelligently, for an answer.

I don't know Elixir, but I assume that the inverse='doc' line in the
relationship sets up an SQLAlchemy backref. If so, then setting
status.doc (presumably done in DocumentStatus.__init__) will
automatically populate doc.statuses at the same time.

So when you do doc.statuses.append(status) a bit later on, you're adding
it to the list a second time.

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Defining a relationship without a foreign key constraint?

2011-03-15 Thread recurse
I'm wondering if there is a way to define a relationship without
creating an associated foreign key constraint in the database.  It
seems like relationship() requires me to define a foreign key, and
that in turn automatically creates a foreign key constraint.  I'm
currently using the declarative syntax to define my tables.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] key error in ColumnCollection

2011-03-15 Thread Michael Bayer

On Mar 15, 2011, at 11:17 AM, nospam wrote:

 I haven't been able to reproduce this issue and only saw it once.  Any
 ideas?  Looks like the list of keys is out of sync w/ the dict in the
 OrderedDict ...  fyi, I don't have a column or property name
 items_type.  However, I do have an inheritence structure for the
 class Item, which has a property and column type.
 
 Traceback (most recent call last):
  File core\threadpool.pyc, line 102, in run
  File gui\maingui\RecentAnnotations.pyc, line 30, in _executeQuery
  File services\AuthClient.pyc, line 19, in __call__
  File soaplib\client.pyc, line 170, in __call__
 Fault:
return iter(self.values())
  File C:\downloads\SQLAlchemy-0.6.1.tar\SQLAlchemy-0.6.1\lib
 \sqlalchemy\util.py, line 818, in values
return [self[key] for key in self._list]
 KeyError: u'items_type'

nothing jumps out there without any kind of example illustrating what causes 
the issue.  0.6.1 also has more bugs than the current release of 0.6.6.


 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Defining a relationship without a foreign key constraint?

2011-03-15 Thread Mike Conley
The foreign key and join condition can be specified as part of the relation
definition without having the foreign key existing in the database

class User(Base):
__tablename__ = 'users'
logon = Column(String(10), primary_key=True)
group_id = Column(Integer)

class Group(Base):
__tablename__ = 'groups'
group_id = Column(Integer, primary_key=True)
group_nm = Column(String(10))
users = relation('User', backref='grp',
primaryjoin='User.group_id==Group.group_id',
foreign_keys='User.group_id')


-- 
Mike Conley

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.