[sqlalchemy] Re: How to diagnose a transaction hang problem?

2010-02-01 Thread
Thanks a lot!

I tried pg_top and found locks been hold by idle transaction, and
then I found that I forgot to close a session!


On Jan 30, 9:43 am, Gunnlaugur Briem gunnlau...@gmail.com wrote:
 Another quick way of troubleshooting hangs is the tool pg_top, in
 which you might see a process in the state “Idle in transaction”. This
 state means that some database operations have been performed in a
 transaction on that connection but the transaction has not yet been
 committed.

 Those database operations will have been granted locks, for which your
 stalled session is waiting. Behind this idle-in-transaction connection
 might be another SQLAlchemy session that you neglected to commit or
 close. That's a common way for this situation to come up.

 You can see the locks held by the connection using pg_top (hit L), or
 you can find them with pg_locks as Alex mentioned. These locks may
 give you a clue as to where in your code that other session was
 created, helping you track down the bug to correct.

 To avoid creating cases like this, I try to be careful about session
 objects: I never store them (keep them on the stack, i.e. as local
 variables and function arguments), and I always create and close them
 using a construct like this:

 from contextlib import closing
 with closing(Session()) as session:
     do_stuff()
     session.commit() if I want to

 Note that sessions are not the same as DB connections (which are
 pooled further down in the layers of stuff going on), you gain nothing
 by storing and reusing them, and you risk creating cases like this.
 Per the docs, “Sessions are very inexpensive to make, and don’t use
 any resources whatsoever until they are first used...so create
 some!” (and close and discard them happily).

     - G.

 On Jan 29, 2:13 pm, Alex Brasetvik a...@brasetvik.com wrote:

  On Jan 29, 2010, at 15:01 , 一首诗 wrote:

   What might cause this kind of problem?

  Possibly waiting on locks. Do you have any concurrent transactions 
  modifying the same data?

  When the problem appears, run `select * from pg_stat_activity` to see 
  whether there are locking issues.

  To see the locks involved, run `select * from pg_locks`.

  --
  Alex Brasetvik

-- 
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: How to diagnose a transaction hang problem?

2010-01-30 Thread
Your advice sound reasonable.

That means I have check the transaction **before** the hanged commit,
maybe I forgot to commit or close a session at that time.

On Jan 31, 7:55 am, Gunnlaugur Briem gunnlau...@gmail.com wrote:
 You probably don't have to add locks in your code. The database system
 takes locks on behalf of your code (in operations initiated by your
 code) as necessary to ensure transactional consistency. That's one of
 the benefits of an RDBMS.

 What you do have to do is to make sure that those database locks don't
 stay around forever (blocking other transactions). That happens when
 you forget to complete (commit or rollback/close) the transaction that
 creates them. If it eventually completes (either way), then things
 will work fine for the most part --- and at least not hang.

 (There is a lot more to this; you should read up on transactions and
 locks, it's fascinating stuff. But your immediate problem is very
 likely just a session/connection that you forget to close.)

 Regards,

     - Gulli

 On Jan 30, 4:53 am, 一首诗 newpt...@gmail.com wrote:

  Yeah, there might be another transaction modifying the same data
  (actually the same line of data in database).

  But I didn't expect that might cause problem before!

  Oh, if that's true, then I have to add some lock in my code to avoid
  that.  That's a big problem.

  On Jan 29, 10:13 pm, Alex Brasetvik a...@brasetvik.com wrote:

   On Jan 29, 2010, at 15:01 , 一首诗 wrote:

What might cause this kind of problem?

   Possibly waiting on locks. Do you have any concurrent transactions 
   modifying the same data?

   When the problem appears, run `select * from pg_stat_activity` to see 
   whether there are locking issues.

   To see the locks involved, run `select * from pg_locks`.

   --
   Alex Brasetvik

-- 
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] How to diagnose a transaction hang problem?

2010-01-29 Thread
Today I met a strange problem with SqlAlchemy and Postgresql.  The
code is like this:


def update_user(user_id, sess):
user = sess.query(User).get(user_id).one()

user.last_activity_time = datetime.now()

session.commit()     It hangs here forever.


In the code above, sess is a scoped session.

I don't have any clue of what happened.  In most case the code above
worked.  But suddenly it hangs and any other thread that want to talk
to database after that line is hit are also hanged.  I have to kill
the process.

It does not look like that this a problem of database since after I
restart my application, it works again.

What might cause this kind of problem?

-- 
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: How to diagnose a transaction hang problem?

2010-01-29 Thread
Yeah, there might be another transaction modifying the same data
(actually the same line of data in database).

But I didn't expect that might cause problem before!

Oh, if that's true, then I have to add some lock in my code to avoid
that.  That's a big problem.

On Jan 29, 10:13 pm, Alex Brasetvik a...@brasetvik.com wrote:
 On Jan 29, 2010, at 15:01 , 一首诗 wrote:

  What might cause this kind of problem?

 Possibly waiting on locks. Do you have any concurrent transactions modifying 
 the same data?

 When the problem appears, run `select * from pg_stat_activity` to see whether 
 there are locking issues.

 To see the locks involved, run `select * from pg_locks`.

 --
 Alex Brasetvik

-- 
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] What might cause QueuePool limit of size 5 overflow 10 reached?

2009-08-13 Thread

I occasionally got QueuePool limit of size 5 overflow 10 reached.

I don't call session.close() after I don't need them.  Because I think
GC will do that.

But now, it seems that I am losting connections somewhere.


Should I call session.close after I use them?

Should I use try.. finally... to make sure session.close is called?
--~--~-~--~~~---~--~~
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] Should we separate business logic and ORM mapping classes.

2009-04-12 Thread

Hi,

Usually, I only use ORM, like pyamf just as an abstraction layer of
database.  So these mapping objects I wrote only contains data but not
behavior.

For example, if I have a class User, I would write another class
UserManager which with a methods like addUser, delUser.  But recently
I am thinking of moving these methods to User.  That sounds more OO
style.

What's your opinion?  What's the benefits and problems of this style?
--~--~-~--~~~---~--~~
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] Re: Any one tried to use SqlAlchemy with XML RPC?

2009-03-19 Thread

The actual reason why I need XML RPC is not because I need RPC.
Actaully I need XML.

I need to represent SA objects in text format so systems using
programming language other than python could decode them.

On Mar 19, 1:27 pm, Andreas Jung li...@zopyx.com wrote:
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 On 19.03.2009 5:24 Uhr, 一首诗 wrote:



  This is my ORM class definition:

  #=
  class User(Base):
  __tablename__ = 'b_taa_user'

  user_id = Column(Integer, primary_key=True, autoincrement=True,
  nullable=False) # 用户ID
  user_name = Column(Unicode(255), unique=True, nullable=False,
  server_default=) # 用户名称
  user_password = Column(Unicode(255), nullable=False,
  server_default=) # 用户密码
  email = Column(Unicode(255), nullable=False, server_default=) # 邮
  箱
  mobile = Column(Unicode(255), nullable=False, server_default=) #
  手机
  phone = Column(Unicode(255), nullable=False, server_default=) # 电
  话

  def __init__(self, user_name = ):
  self.user_name = username
  #=

  I made some debug by the code below:

  #=

  #!/usr/bin/python

  import xmlrpclib
  from blf.model import User

  u = User('333')
  s = xmlrpclib.dumps((u,), methodresponse=True, allow_none=True)
  print s
  #=

  Finally, I found that the first problem is :  xmlrpclib only serialize
  type *instance*.
  But to my surprise, when I check type of u, it's a *class* object.
  Don't know why ...

 Sending mapped instances over XMLRPC is basically nonsense. XMLRPC is
 designed for transporting elementary datatypes over the wire in order to
 invoke a remote method. It is not designed for sending instances of some
 class and not for sending Python pickles. No idea what you are trying to
 accomplish. Consider converting a mapped instance to dict and sending
 this dict serialized in JSON format to a remote XMLRPC server.

 - -aj
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.9 (Darwin)
 Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/

 iEYEARECAAYFAknB17UACgkQCJIWIbr9KYwARgCeJ1VuU6Gi9p+GVkYQzZiVWMd3
 aJoAoMwJUc8lIyOBhDwJVq3/L3QzuRoK
 =aihX
 -END PGP SIGNATURE-

  lists.vcf
  1KViewDownload
--~--~-~--~~~---~--~~
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] Re: Any one tried to use SqlAlchemy with XML RPC?

2009-03-18 Thread

This is my ORM class definition:

#=
class User(Base):
__tablename__ = 'b_taa_user'

user_id = Column(Integer, primary_key=True, autoincrement=True,
nullable=False) # 用户ID
user_name = Column(Unicode(255), unique=True, nullable=False,
server_default=) # 用户名称
user_password = Column(Unicode(255), nullable=False,
server_default=) # 用户密码
email = Column(Unicode(255), nullable=False, server_default=) # 邮
箱
mobile = Column(Unicode(255), nullable=False, server_default=) #
手机
phone = Column(Unicode(255), nullable=False, server_default=) # 电
话

def __init__(self, user_name = ):
self.user_name = username
#=

I made some debug by the code below:

#=

#!/usr/bin/python

import xmlrpclib
from blf.model import User

u = User('333')
s = xmlrpclib.dumps((u,), methodresponse=True, allow_none=True)
print s
#=

Finally, I found that the first problem is :  xmlrpclib only serialize
type *instance*.
But to my surprise, when I check type of u, it's a *class* object.
Don't know why ...




On Mar 18, 6:50 pm, Noah Gift noah.g...@gmail.com wrote:
 On Wed, Mar 18, 2009 at 11:46 PM, 一首诗 newpt...@gmail.com wrote:

  Hi,

  I am trying to pass SqlAlchemy objects to client with XML RPC, but it
  seems that that's not very easy.

 It depends on what you are trying to do, and what error messages you are
 getting, for anyone to help.  I am using XML RPC with SQLAlchemy right now,
 and I am, in fact, passing objects in.  I wrote a metaclass that calls back
 to XML RPC, and takes the ugly nested dictionary and returns back a nifty
 class pre-populated with attributes I need to fill out.  When I set each
 attribute, it then populates __dict__.  At that point, I simply pass that
 back to XML RPC.  Works great.



  The reason why I have to do this is that my client is written in
  javascript and running on a web page.  So it could not use anything
  other than text based protocols, such as XML RPC, SOAP, or JSON.

  I tried pyamf, and it works pretty good with SA,  but I can't use amf
  with javascript.

  Is there any possible solutions with my problem?

 --
 Cheers,

 Noah
--~--~-~--~~~---~--~~
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] Re: Using sqlalchemy in twisted.

2009-03-11 Thread

Hi Jeff,

In my project I use the *model* to indicate an instance of Database.
I don't really need multiple instances of Database.  But when I wrote
unit test, I always create an new one database in setup, which create
a new sqlite in memory database to avoid conflicts between test cases.

About the trick to make *sess* a keywork parameter,  that's really
clever!
Thanks a lot!

On Mar 11, 9:05 pm, Jeff FW jeff...@gmail.com wrote:
 Logging SA objects *after* the session is gone will always be a
 problem, unless you make sure to detach all of them from the session.
 I'd just log the original request, instead.  In my case, I have to
 convert all of my SA objects to something Perspective Broker
 understands, so I actually log those *after* that, as they're no
 longer part of the session--but I'm not sure if you can do that in
 your case.

 As for the decorator--I got a little confused with your names--you
 call it model in your decorator, but it's really an instance of
 Database when it gets passed in as self.  One way to get rid of that
 parameter would be to make sess a keyword argument, like so:

 def wrapper(*args, **kw):
 sess = model.Session()
 try:
 return f(sess=sess, *args, **kw)

 and then change your method:

 def _getObjectById(self, klass, id, sess=None):
 return sess.query(klass).get(id)

 That way, self will get passed in *args with no problem.

 Are you planning to have multiple instances of your Database class?
 If not, I'd suggest changing everything in it into class methods, so
 that way you can call it *without* an instance at all, and don't have
 to worry about connecting to the database multiple times by accident.
 Just a thought.

 -Jeff

 On Mar 10, 10:38 am, 一首诗 newpt...@gmail.com wrote:

  Hi Jeff,

  Thanks for your kind suggestion.

  I first add some log decorators, but i found when it might cause to
  print sqalchemy objects which has not been bound to any session.

  And I am not quite sure about how to make the decorator mor genreal.

  Actually, I think I must use model as the first parameter because as a
  instance method, _getObjectById require the first parameter to be
  self.
  Can you write a few lines of code to show your suggestion?

  On Mar 8, 5:06 am, Jeff FW jeff...@gmail.com wrote:

   That's pretty similar to what I do, actually, if a bit simpler (but
   that's good!)  One suggestion would be to throw an except (maybe for
   the base SQLAlchemy exception class)  in your try block, otherwise you
   run the risk of things dying in an ugly way.  I'm not familiar with
   pyamf, so I don't know how it would handle errors, but twisted usually
   carries on as if nothing happens.

   Also, I'd make the decorator a bit more general--don't put the model
   argument in wrapper().  Put sess first, then take *args and **kwargs,
   and pass those right to the inner function f(). That way you can reuse
   it for anything that requires a DB session.

   Other things you could add (if so inclined) are decorators for logging
   and other types of error handling (like catching IntegrityErros thrown
   by duplicates.)  I do those things, but I might be a bit OCD :-)

   -Jeff

   On Mar 7, 1:41 am, 一首诗 newpt...@gmail.com wrote:

Hi, Thanks for your reply.  I'm using it the way like you.  The only
difference is that I am using pyamf instead of PB.

On every request, I delegate required db operations to a class called
Database, similar to these code below.

I used to use scope_session instead of create and close session every
time.   But as I said in my earlier mails, they don't work.

These code below seems to work right now.  But if you have more
suggestion,  I will be very thankful.

#=

def require_session(f):
'''create and close session for each synchronous method'''
def wrapper(model, *args, **kw):
sess = model.Session()
try:
return f(model, sess, *args, **kw)
finally:
sess.close()
return wrapper

class Database()
def __init__(self, conn_str):
self.conn_str = conn_str
self.engine = create_engine(self.conn_str, echo=False)
self.Session = sessionmaker(bind = self.engine,
expire_on_commit=False)

def getObjectById(self, klass, id):
return threads.deferToThread(self._getObjectById, klass, id)

@require_session

def _getObjectById(self, sess, klass, id):

return sess.query(klass).get(id)
#=

On Mar 6, 5:44 am, Jeff FW jeff...@gmail.com wrote:

 Don't use scoped_session--you'll run into problems no matter what you
 do.  I'm using Perspective Broker from Twisted with SQLAlchemy.  I
 make sure to create and commit/rollback a session for *every* PB
 request.  It works perfectly, and that's the only way I

[sqlalchemy] Re: Using sqlalchemy in twisted.

2009-03-10 Thread

Hi Jeff,

Thanks for your kind suggestion.

I first add some log decorators, but i found when it might cause to
print sqalchemy objects which has not been bound to any session.

And I am not quite sure about how to make the decorator mor genreal.

Actually, I think I must use model as the first parameter because as a
instance method, _getObjectById require the first parameter to be
self.
Can you write a few lines of code to show your suggestion?

On Mar 8, 5:06 am, Jeff FW jeff...@gmail.com wrote:
 That's pretty similar to what I do, actually, if a bit simpler (but
 that's good!)  One suggestion would be to throw an except (maybe for
 the base SQLAlchemy exception class)  in your try block, otherwise you
 run the risk of things dying in an ugly way.  I'm not familiar with
 pyamf, so I don't know how it would handle errors, but twisted usually
 carries on as if nothing happens.

 Also, I'd make the decorator a bit more general--don't put the model
 argument in wrapper().  Put sess first, then take *args and **kwargs,
 and pass those right to the inner function f(). That way you can reuse
 it for anything that requires a DB session.

 Other things you could add (if so inclined) are decorators for logging
 and other types of error handling (like catching IntegrityErros thrown
 by duplicates.)  I do those things, but I might be a bit OCD :-)

 -Jeff

 On Mar 7, 1:41 am, 一首诗 newpt...@gmail.com wrote:



  Hi, Thanks for your reply.  I'm using it the way like you.  The only
  difference is that I am using pyamf instead of PB.

  On every request, I delegate required db operations to a class called
  Database, similar to these code below.

  I used to use scope_session instead of create and close session every
  time.   But as I said in my earlier mails, they don't work.

  These code below seems to work right now.  But if you have more
  suggestion,  I will be very thankful.

  #=

  def require_session(f):
  '''create and close session for each synchronous method'''
  def wrapper(model, *args, **kw):
  sess = model.Session()
  try:
  return f(model, sess, *args, **kw)
  finally:
  sess.close()
  return wrapper

  class Database()
  def __init__(self, conn_str):
  self.conn_str = conn_str
  self.engine = create_engine(self.conn_str, echo=False)
  self.Session = sessionmaker(bind = self.engine,
  expire_on_commit=False)

  def getObjectById(self, klass, id):
  return threads.deferToThread(self._getObjectById, klass, id)

  @require_session

  def _getObjectById(self, sess, klass, id):

  return sess.query(klass).get(id)
  #=

  On Mar 6, 5:44 am, Jeff FW jeff...@gmail.com wrote:

   Don't use scoped_session--you'll run into problems no matter what you
   do.  I'm using Perspective Broker from Twisted with SQLAlchemy.  I
   make sure to create and commit/rollback a session for *every* PB
   request.  It works perfectly, and that's the only way I was really
   able to get it to work in all cases.

   Assuming you're using Twisted in a similar way, you could write a
   simple decorator to wrap any functions that need a database session in
   the begin/commit stuff as necessary.

   If you can give more details of how you're using Twisted, I might be
   able to offer some more insight.

   -Jeff

   On Mar 5, 12:33 am, 一首诗 newpt...@gmail.com wrote:

I'm not quite sure, but I think I'm pretty careful of sharing objects
between threads.

1st, I only cached as few as possible orm objects.  I tried to detach
them, but I found that if I detach them,  I can't access any of their
fields any more.

2nd, I create new orm objects based on client request, pass them to
class Database and then merge them to scoped sessions, change, commit
and then discard these objects.

3rd, I switch to sqlite frequently to check if there is any database
operation outside Database, because sqlite doesn't allow multi-thread
access.

Actually it seems to work until 2 or 3 days ago suddenly cases hang
the server.

Ah, as I've already written lots of code in ORM, I think maybe I
should try to change Database to use a dedicated thread to handle all
database operations.

That might be a bottle neck of my application, but I really can't give
up orm as these mapper classes are used everywhere in my application.

On Mar 4, 7:26 pm, 一首诗 newpt...@gmail.com wrote:

 Hi, all

 I am using sqlalchemy in twisted in my project in the way below.
 Defer any database operation so the twisted's main thread won't be
 blocked.

 And I use scoped_session, so that sessions won't have to be created
 again and again.

 ==
 class Database()
 def __init__(self, conn_str):
 self.conn_str = conn_str

[sqlalchemy] Re: Using sqlalchemy in twisted.

2009-03-04 Thread

I'm not quite sure, but I think I'm pretty careful of sharing objects
between threads.

1st, I only cached as few as possible orm objects.  I tried to detach
them, but I found that if I detach them,  I can't access any of their
fields any more.

2nd, I create new orm objects based on client request, pass them to
class Database and then merge them to scoped sessions, change, commit
and then discard these objects.

3rd, I switch to sqlite frequently to check if there is any database
operation outside Database, because sqlite doesn't allow multi-thread
access.

Actually it seems to work until 2 or 3 days ago suddenly cases hang
the server.

Ah, as I've already written lots of code in ORM, I think maybe I
should try to change Database to use a dedicated thread to handle all
database operations.

That might be a bottle neck of my application, but I really can't give
up orm as these mapper classes are used everywhere in my application.

On Mar 4, 7:26 pm, 一首诗 newpt...@gmail.com wrote:
 Hi, all

 I am using sqlalchemy in twisted in my project in the way below.
 Defer any database operation so the twisted's main thread won't be
 blocked.

 And I use scoped_session, so that sessions won't have to be created
 again and again.

 ==
 class Database()
 def __init__(self, conn_str):
 self.conn_str = conn_str
 self.engine = create_engine(self.conn_str, echo=False)
 self.Session = scoped_session(sessionmaker(bind = self.engine,
  expire_on_commit=False))

 def getObjectById(self, klass, id):
 return threads.deferToThread(self._getObjectById, klass, id)

 def _getObjectById(self, klass, id):
 sess = self.Session()
 return sess.query(klass).get(id)
 ==

 The code doesn't work.   When I limit the thread numbers to 1

 reactor.suggestThreadPoolSize(1)

 Everything goes fine.  Other wise the server would be blocked and must
 be killed by kill 9 

 The result conflicts with my understanding of sqlalchemy.  Since I
 don't share any object between threads, there should be no problem!

 Ah  It always have risk to use something you haven't tried
 before 
--~--~-~--~~~---~--~~
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] An instance's attributes lost while the other updated.

2009-02-25 Thread

Hi all,

I have 2 user instance in a session and after I have update one of it
like this

---

u = session.merge(u)
session.commit
session.refresh(u)

---

Later, I found that another instance of the same class type in the
session's attributes was cleared, and triggered a database query.

(I found this because I am using sqlite, which forbids access the same
database in more than one thread)

I will make some debug to find out what happened.
--~--~-~--~~~---~--~~
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] What's the use of expunge?

2009-02-25 Thread

The document says:


 Expunge removes an object from the Session, sending persistent
instances to the detached state, and pending instances to the
transient state:



I hoped that if an instance was expunged from a session, I might use
it safely as a container of some temp data without fearing trigger any
database query.

But I found that if an object is 'expunge'd ,  it's properties will
not be accessible anymore.

So, when should we use expunge?  Or it is kept for sqlalchemy inner
use only?
--~--~-~--~~~---~--~~
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] Re: An instance's attributes lost while the other updated.

2009-02-25 Thread

A little more code to explain:

Before commit, print u2 doesn't do trigger query.
After commit, print u2 trigger a query, even if it has different
primary key with u1.

So this means : when u update one instance, the other instance of the
same type will be in expired state.

##
u1 = session.merge(u1)

print --
print u2

session.commit()

print --
print us
##

On Feb 26, 2:17 pm, 一首诗 newpt...@gmail.com wrote:
 Hi all,

 I have 2 user instance in a session and after I have update one of it
 like this

 --- 
 

 u = session.merge(u)
 session.commit
 session.refresh(u)

 --- 
 

 Later, I found that another instance of the same class type in the
 session's attributes was cleared, and triggered a database query.

 (I found this because I am using sqlite, which forbids access the same
 database in more than one thread)

 I will make some debug to find out what happened.
--~--~-~--~~~---~--~~
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] How to write a self 2 self relationship?

2009-02-16 Thread

I tried to write :

#
class User(Base):
__tablename__ = 'users'

id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
sons = relation(User, order_by=User.id, backref=parent)
#

But the as 'User' is not defined at the line ... relation ...  is
processed, the code above doesn't work.

So, does sqlalchemy support self 2 self relationship ?
If the answer is YES, how to do it?
--~--~-~--~~~---~--~~
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] Re: How to write a self 2 self relationship?

2009-02-16 Thread

Like this ?

class User(Base):
__tablename__ = 'users'

id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
sons = relation('User', order_by='User.id', backref=parent)



I got an Exception:

sqlalchemy.exc.ArgumentError: Could not determine join condition
between parent/child tables on relation User.sons.  Specify a
'primaryjoin' expression.  If this is a many-to-many relation,
'secondaryjoin' is needed as well.


On Feb 16, 5:08 pm, a...@svilendobrev.com wrote:
 put it as text, it will be eval()'uated later

 On Monday 16 February 2009 10:57:11 一首诗 wrote:

  I tried to write :

  #--
 -- class User(Base):
      __tablename__ = 'users'

      id = Column(Integer, primary_key=True)
      name = Column(String)
      fullname = Column(String)
      password = Column(String)
      sons = relation(User, order_by=User.id, backref=parent)
  #--
 --

  But the as 'User' is not defined at the line ... relation ...  is
  processed, the code above doesn't work.

  So, does sqlalchemy support self 2 self relationship ?
  If the answer is YES, how to do it?
--~--~-~--~~~---~--~~
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] Problem of Unicode Strings

2009-02-11 Thread

Hi all,

I'm using sqlalchemy as my ORM library and it works fines until today
one of my test case fail

===
sqlalchemy.exc.ProgrammingError: (ProgrammingError) You must not use
8-
bit bytestrings unless you use a text_factory that can interpret 8-bit
bytestrings (like text_factory = str). It is highly recommended that
you instead just switch your application to Unicode strings. u'INSERT
INTO b_taa_role (role_name, activity, grantable, hierarchy, creator,
related_smf, related_domain, priority) VALUES
(?, ?, ?, ?, ?, ?, ?, ?)' ['\xe7\xae\xa1\xe7\x90\x86\xe5\x91\x98',
None, None, None, None, None, None, 0]
===

The problem is : it only fails on one system.

I've test the case in 3 computer, 2 ubuntu and 1 ubuntu server.

It only fails on ubuntu server edition.

The locale setting, python version, and sqlalchemy version is
identical!
As the final running environment is not decided yer,  I am afraid this
will be a big problem.

I have considered to use unicode every where,  but the problem is,
most of the data will retrieved will be in utf8.  So if I choose
unicode, I have to translate every string parameter.

--~--~-~--~~~---~--~~
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] You must not use 8-bit bytestrings ??

2009-02-11 Thread

Hi all,

I'm using sqlalchemy as my ORM library and it works fines until today
one of my test case fail

===
sqlalchemy.exc.ProgrammingError: (ProgrammingError) You must not use 8-
bit bytestrings unless you use a text_factory that can interpret 8-bit
bytestrings (like text_factory = str). It is highly recommended that
you instead just switch your application to Unicode strings. u'INSERT
INTO b_taa_role (role_name, activity, grantable, hierarchy, creator,
related_smf, related_domain, priority) VALUES
(?, ?, ?, ?, ?, ?, ?, ?)' ['\xe7\xae\xa1\xe7\x90\x86\xe5\x91\x98',
None, None, None, None, None, None, 0]
===

The problem is : it only fails on one system.

I've test the case in 3 computer, 2 ubuntu and 1 ubuntu server.

It only fails on ubuntu server edition.

The locale setting, python version, and sqlalchemy version is
identical!
As the final running environment is not decided yer,  I am afraid this
will be a big problem.

I have considered to use unicode every where,  but the problem is,
most of the data will retrieved will be in utf8.  So if I choose
unicode, I have to translate every string parameter.

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