Re: [sqlalchemy] object has no attribute 'delete'

2010-04-13 Thread Fernando Takai
Hi,

You can try:

My.query.filter_by(id=id).delete()
(If your object can use the .query syntax)
or
session.query(My).filter_by(id=id).delete()

On Apr 13, 2010 6:12 AM, jo jose.soa...@sferacarta.com wrote:

Hi all,

I'm trying migrate from 0.3 to 0.6
I don't know how to delete an object

in the old version it was:

My.get(1).delete()

in 0.6:

My.get(1).delete()

AttributeError: 'My' object has no attribute 'delete'

j

-- 
Jose Soares
Sferacarta Net Via Bazzanese 69
40033 Casalecchio di Reno
Bologna - Italy
Ph  +39051591054
fax +390516131537
web:www.sferacarta.com

Le informazioni contenute nella presente mail ed in ogni eventuale file
allegato sono riservate e, comunque, destinate esclusivamente alla persona o
ente sopraindicati, ai sensi del decreto legislativo 30 giugno 2003, n. 196.
La diffusione, distribuzione e/o copiatura della mail trasmessa, da parte di
qualsiasi soggetto diverso dal destinatario, sono vietate. La correttezza,
l’integrità e la sicurezza della presente mail non possono essere garantite.
Se avete ricevuto questa mail per errore, Vi preghiamo di contattarci
immediatamente e di eliminarla. Grazie.

This communication is intended only for use by the addressee, pursuant to
legislative decree 30 June 2003, n. 196. It may contain confidential or
privileged information. You should not copy or use it to disclose its
contents to any other person. Transmission cannot be guaranteed to be
error-free, complete and secure. If you are not the intended recipient and
receive this communication unintentionally, please inform us immediately and
then delete this message from your system. Thank you.

-- 
You received this message because you are subscribed to the Google Groups
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.comsqlalchemy%2bunsubscr...@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 sqlalch...@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] 'NoneType' object has no attribute '_sa_iterator' exception

2010-03-27 Thread Fernando Takai
Hi all!

I'm running SQLAlchemy 0.6b2 for a while and i've seem some strange
exception (AttributeError: 'NoneType' object has no attribute
'_sa_iterator') happening one in a while.
This is my stacktrace:

Traceback (most recent call last):
  File /usr/lib/python2.5/threading.py, line 486, in __bootstrap_inner
self.run()
  File /usr/lib/python2.5/threading.py, line 446, in run
self.__target(*self.__args, **self.__kwargs)
  File ./db/models/job.py, line 109, in run
func(self)
  File ./queue/queue.py, line 284, in job_finished
job = session.merge(job)
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
line 1126, in merge
load=load, _recursive=_recursive)
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
line 1188, in _merge
prop.merge(self, state, state_dict, merged_state, merged_dict,
load, _recursive)
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/properties.py,
line 681, in merge
obj = session._merge(current_state, current_dict, load=load,
_recursive=_recursive)
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
line 1188, in _merge
prop.merge(self, state, state_dict, merged_state, merged_dict,
load, _recursive)
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/properties.py,
line 661, in merge
for current in instances:
  File 
/usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/collections.py,
line 570, in __iter__
return iter(getattr(self._data(), '_sa_iterator')())
AttributeError: 'NoneType' object has no attribute '_sa_iterator'

I don't have a good test for this, but i'm trying to create one.
Does anyone knows why this is happening?

Thanks!

-- 
Fernando Takai

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] 'NoneType' object has no attribute '_sa_iterator' exception

2010-03-27 Thread Fernando Takai
 is it possible your object is present in a session that is, in a different 
 thread, being expired as this operation runs ?
I don't know, but i can verify that.
If that is happening, what can i do to prevent?


 Well seems like the project you want to access is just None. SA can't do 
 anything with it. I had a similar problem recently, it was…
I'm not trying to access anything, i'm just trying to merge the object
into the session…

On Sat, Mar 27, 2010 at 3:25 PM, Michael Bayer mike...@zzzcomputing.com wrote:

 On Mar 27, 2010, at 12:42 PM, Fernando Takai wrote:

 Hi all!

 I'm running SQLAlchemy 0.6b2 for a while and i've seem some strange
 exception (AttributeError: 'NoneType' object has no attribute
 '_sa_iterator') happening one in a while.
 This is my stacktrace:

 Traceback (most recent call last):
  File /usr/lib/python2.5/threading.py, line 486, in __bootstrap_inner
    self.run()
  File /usr/lib/python2.5/threading.py, line 446, in run
    self.__target(*self.__args, **self.__kwargs)
  File ./db/models/job.py, line 109, in run
    func(self)
  File ./queue/queue.py, line 284, in job_finished
    job = session.merge(job)
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
 line 1126, in merge
    load=load, _recursive=_recursive)
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
 line 1188, in _merge
    prop.merge(self, state, state_dict, merged_state, merged_dict,
 load, _recursive)
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/properties.py,
 line 681, in merge
    obj = session._merge(current_state, current_dict, load=load,
 _recursive=_recursive)
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/session.py,
 line 1188, in _merge
    prop.merge(self, state, state_dict, merged_state, merged_dict,
 load, _recursive)
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/properties.py,
 line 661, in merge
    for current in instances:
  File 
 /usr/lib/python2.5/site-packages/SQLAlchemy-0.6beta2-py2.5-linux-x86_64.egg/sqlalchemy/orm/collections.py,
 line 570, in __iter__
    return iter(getattr(self._data(), '_sa_iterator')())
 AttributeError: 'NoneType' object has no attribute '_sa_iterator'

 is it possible your object is present in a session that is, in a different 
 thread, being expired as this operation runs ?







 I don't have a good test for this, but i'm trying to create one.
 Does anyone knows why this is happening?

 Thanks!

 --
 Fernando Takai

 --
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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 sqlalch...@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.





-- 
Fernando Takai

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] Re: SQLAlchemy, Twisted, and sAsync

2010-03-26 Thread Fernando Takai
Hi!,

I'm using SQLAlchemy on a heavily threaded env - something like 30~40
threads working with SQLAlchemy objects.
What you need to watchout is:

* Eager load objects - getting nasty lazyload exceptions is not funny
* Take off the objects from the session and, if you need to use them
later, merge to the current thread session - i did this because i had
some object-is-already-on-session-foobar exception
* Don't forget to use scoped_session

Beside those two points, working with threaded apps is quite easy with
SQLAlchemy. :)

On Fri, Mar 26, 2010 at 10:12 AM, Matthew Williams mgwilli...@gmail.com wrote:
 Thank you, Simon, for clarifying this and pointing out that part of the
 SQLAlchemy docs... somehow I missed that part :-).

 On Mar 26, 2010, at 7:30 AM, King Simon-NFHD78 wrote:

 I think that point should be clarified, so that people don't later come
 across this post and just accept it without understanding.

 I imagine that SQLALchemy is used in a lot of threaded applications. For
 example, it is the recommended ORM in web frameworks such as Pylons and
 TurboGears, which work fine in threaded environments. However, typically
 in these libraries a web request is handled by a single thread, and all
 the SQLAlchemy operations occur within the scope of that request. As
 long as you don't share a Session instance between the threads, you
 won't have any problems. SQLAlchemy provides a ScopedSession class which
 helps in these situations, as you can call the constructor many times on
 a single thread and always get the session instance back for that
 thread. Sessions themselves aren't thread-safe.

 When an instance is loaded from the database, it is linked to the
 session that loaded it. This means that when you have lazy-loading
 properties on that instance (such as related classes, or deferred column
 properties), they will be automatically loaded when they are accessed,
 in the same session.

 This will cause a problem if you load an instance in thread A, hand the
 object off to thread B, and then thread B accesses one of these
 lazy-loading properties. The load will occur in thread A's session,
 which might be in the middle of doing something else.

 The solution to this is either to eager-load all the attributes you
 think you are going to need before handing the instance off to another
 thread (difficult), or (probably better) to detach (expunge) the
 instance from thread A's session. Thread B should then merge the object
 into its own session (using the load=False flag so that it doesn't
 needlessly requery the database).

 The Session docs at http://www.sqlalchemy.org/docs/session.html explain
 the lifecycle of loaded instances.

 I haven't actually done any of this - I've only ever used SA from TG and
 command-line scripts, but I think the principles are about right. I 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 sqlalch...@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.





-- 
Fernando Takai

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] Re: UnboundExecutionError with object on session

2009-11-18 Thread Fernando Takai
This is the exact code that is running now:

log.info(Creating proxies for file %s % job.file)

job = session.merge(job)

if agent.transcoder.run(job.local_filename):
log.info(Transcoding finished for file %s % job.local_filename)
else:
log.error(Occurred a problem while transcoding the file. Please, verify 
the logs.)
job.error_status = Occurred a problem while transcoding the file. Please, 
verify the logs.

(The problem is occurring on the log.info line)
As you can see, i *am* merging the job on the current session - so, the 
exception shouldn't be happening.

Also, i use the local_filename variable before, so it's not expired (afaik).

On Nov 17, 2009, at 1:35 PM, Michael Bayer wrote:

 
 Fernando Takai wrote:
 
 I have changed my code to be like this:
 
 job = session.merge(job)
 # Merge docs says that object does not get into the session
 session.add(job)
 
 log.info(Job finished! %s % job.file)
 
 When using latest SQLAlchemy trunk and 0.5.6 sometimes i get
 UnboundExecutionError.
 
 Also, i can assure that all my attributes are loaded - i use all of them
 before and no error occurs.
 
 but they may be expired at some point.  If their host session is rolled
 back or committed, for example.   The stack trace will illustrate exactly
 what action is initiating the load operation. For example, in the stack
 trace you posted, the job.local_filename attribute is unloaded or
 expired.  You can test for this by seeing that local_filename is not
 present in job.__dict__.
 
 
 
 Maybe there's something wrong with the way i'm getting the session:
 
 session = Session.object_session(job)
 if not session:
session = Session()
 
 there's nothing wrong with it per se except it appears to be guessing as
 to what session should be used - well lets see if this object has a
 session from somewhere, or otherwise we'll just make one, maybe not.
 
 Sessions can be created as much as you like but they work best when they
 are in charge of their scope, i.e.:
 
 def do_something():
# build the one session we care about for this thread/operation
sess = session()
 
# ensure everyone from outside is merged into our one session
o1 = sess.merge(o1)
o2 = sess.merge(o2)
o3 = sess.merge(o3)
 
# commit with authority
sess.commit()
 
 
 as opposed to:
 
 def do_something():
s1 = object_session(o1) or Session()
s2 = object_session(o2) or Session()
s3 = object_session(o3) or Session()
 
s3.commit()
s2.commit() ?  or no ?  where did this session come from ?
 
 
 
 
 
 
 --~--~-~--~~~---~--~~
 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
 -~--~~~~--~~--~--~---
 

--
Fernando Takai
http://twitter.com/fernando_takai





--

You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] Re: UnboundExecutionError with object on session

2009-11-17 Thread Fernando Takai

I have changed my code to be like this:

job = session.merge(job)
# Merge docs says that object does not get into the session
session.add(job)

log.info(Job finished! %s % job.file)

When using latest SQLAlchemy trunk and 0.5.6 sometimes i get 
UnboundExecutionError.

Also, i can assure that all my attributes are loaded - i use all of them before 
and no error occurs.

Maybe there's something wrong with the way i'm getting the session:

session = Session.object_session(job)
if not session:
session = Session()

On Nov 16, 2009, at 3:27 PM, Michael Bayer wrote:

 
 Fernando Takai wrote:
 
 Hi all!
 
 I've experiencing this problem for some time now and even after
 debugging, i could not find why it happens.
 
 I have a medium sized multi-thread application that manipulates
 SQLAlchemy objects - the objects are passed from thread to thread, so,
 when i load an instance i close the session.
 
 After working on the object, i need to update the status of the it, so
 i do something like this:
 
 job.status = FINISHED
 session.add(job)
 log.info(Finished job %s % job.filename)
 session.flush()
 
 This code works pretty well, but from time to time, i get this on the
 logs:
 
 UnboundExecutionError: Instance Job at 0x413ee50 is not bound to a
 Session; attribute refresh operation cannot proceed
 
 The complete stacktrace is here: http://pastebin.org/54196
 
 Could this be happening because of my pool_recycle setting (300
 seconds) ?
 
 its not related to the pool.  Your objects have expired or unloaded
 attributes present on them which will attempt to load when accessed.   The
 object must be attached to a Session for this to proceed.
 
 the easiest way to deal with this is to merge() the objects into a new
 thread-local Session before using.
 
 Alternatively, ensure all required attributes are loaded.  This often
 requires touching the attributes explicitly in the case of joined table
 inheritance or lazily-loaded relations().   Also note that calling
 session.commit() or session.rollback() expires all attributes, so avoid
 these in the case of objects becoming detached, or expunge() the objects
 before an expiration occurs.
 
 
 
  

--
Fernando Takai
http://twitter.com/fernando_takai






--~--~-~--~~~---~--~~
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] UnboundExecutionError with object on session

2009-11-16 Thread Fernando Takai

Hi all!

I've experiencing this problem for some time now and even after
debugging, i could not find why it happens.

I have a medium sized multi-thread application that manipulates
SQLAlchemy objects - the objects are passed from thread to thread, so,
when i load an instance i close the session.

After working on the object, i need to update the status of the it, so
i do something like this:

job.status = FINISHED
session.add(job)
log.info(Finished job %s % job.filename)
session.flush()

This code works pretty well, but from time to time, i get this on the
logs:

UnboundExecutionError: Instance Job at 0x413ee50 is not bound to a
Session; attribute refresh operation cannot proceed

The complete stacktrace is here: http://pastebin.org/54196

Could this be happening because of my pool_recycle setting (300
seconds) ?

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