[sqlalchemy] with_polymorphic mapper option for future classes

2011-07-20 Thread Torsten Landschoff
Hi *,

is there a way to use the with_polymorphic mapper option for classes yet
to be defined?

My problem is that I have a base class for a number of different
database objects. There are few specific leafs classes of which many
instances are loaded at once.

Currently, I pass with_polymorphic=(*, None) while mapping, but that
creates huge query for little effect. I would like to pass the two
classes I am interested in but they are defined in other modules (which
depends on the module where I map the base class).

Is there a way to update the with_polymorphic setting on the base class
when mapping the derived class?

Greetings, Torsten

-- 
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff

Office Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561

mailto:torsten.landsch...@dynamore.de
http://www.dynamore.de

Registration court: Mannheim, HRB: 109659, based in Karlsruhe,
Managing director:  Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz

-- 
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 get last record from a resultset

2011-07-20 Thread Krishnakant Mane

Hello all,
Subject line says it all.
Basically what I want to do is to get last record from a result set.
I am dealing with a situation where given a date I need to know the last 
record pertaining to  transaction on a given account.

yes, it is an accounting/ book keeping software.
So  I thought there was some thing like .last() method for a resultset?
Or even better do we have some thing like session.query(table).last()
The problem is that my logic is in place but I know that performance 
wise it is very dirty to get the list of all records, just to loop till 
the end and throw away all the rest of the rows.

So plese suggest how can I only get just that one (last) record?
Happy hacking.
Krishnakant.

--
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] with_polymorphic mapper option for future classes

2011-07-20 Thread Michael Bayer
perhaps we'd want to establish mapper.with_polymorphic as a setter that will 
perform the requisite validations on it whenever it is set.I think you can 
set it right now on the mapper at any point, it doesn't seem to be consulted in 
any configurational way.   The only validations that occur are on the second 
argument if it is not None.

Give that a try first.   

There is a hackish recipe we have at 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeAbstractConcreteBase
 which illustrates how to set up a mapper with declarative where the mapper() 
is created later, but it would be nice if we could provide the callable 
approach of relationship() for things like this at some point in the future.

Also I don't like with_polymorphic very much but I don't have a better idea at 
the moment.


On Jul 20, 2011, at 8:36 AM, Torsten Landschoff wrote:

 Hi *,
 
 is there a way to use the with_polymorphic mapper option for classes yet
 to be defined?
 
 My problem is that I have a base class for a number of different
 database objects. There are few specific leafs classes of which many
 instances are loaded at once.
 
 Currently, I pass with_polymorphic=(*, None) while mapping, but that
 creates huge query for little effect. I would like to pass the two
 classes I am interested in but they are defined in other modules (which
 depends on the module where I map the base class).
 
 Is there a way to update the with_polymorphic setting on the base class
 when mapping the derived class?
 
 Greetings, Torsten
 
 -- 
 DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
 Torsten Landschoff
 
 Office Dresden
 Tel: +49-(0)351-4519587
 Fax: +49-(0)351-4519561
 
 mailto:torsten.landsch...@dynamore.de
 http://www.dynamore.de
 
 Registration court: Mannheim, HRB: 109659, based in Karlsruhe,
 Managing director:  Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz
 
 -- 
 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] Occasional ProgrammingError in threaded environment

2011-07-20 Thread Michael Bayer

On Jul 20, 2011, at 1:50 AM, Arthur Kopatsy wrote:

 Hi,
 
 My web application has a controller that spawns multiple threads using
 a thread pool. Each of them has its own session (I store it in thread
 local). Occasionally (1 out of 100 or more)  I get really strange
 errors that really look like corrupted queries.
 
 My guess is that it may be due to my thread pooling and the fact that
 a session may be reused for multiple jobs.

Yes this is most likely basic sharing of a Session and/or related state between 
threads.  The stack trace specifically is the access of an unloaded or expired 
attribute on a persistent object - this consults the Session to refresh the 
object.Accesses like these need to be kept local to a single thread.


-- 
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] how to get last record from a resultset

2011-07-20 Thread Timuçin Kızılay

I think, reversing the sort and getting the first record will do.



20-07-2011 16:32, Krishnakant Mane yazmış:

Hello all,
Subject line says it all.
Basically what I want to do is to get last record from a result set.
I am dealing with a situation where given a date I need to know the last
record pertaining to transaction on a given account.
yes, it is an accounting/ book keeping software.
So I thought there was some thing like .last() method for a resultset?
Or even better do we have some thing like session.query(table).last()
The problem is that my logic is in place but I know that performance
wise it is very dirty to get the list of all records, just to loop till
the end and throw away all the rest of the rows.
So plese suggest how can I only get just that one (last) record?
Happy hacking.
Krishnakant.



--
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] how to get last record from a resultset

2011-07-20 Thread Krishnakant Mane
Well, there won't be a consistent result using sort because there might 
be 10 rows with same voucher code and same account code.

That's exactly the challenge so I don't know how sort will help.
If we can invert the entire resultset having the last record become 
first,  then its worth while.

But again, I don't want the entire set of rows in the first place.
I just want that particular row.
Happy hacking.
Krishnakant.

On 20/07/11 19:20, Timuçin Kızılay wrote:

I think, reversing the sort and getting the first record will do.



20-07-2011 16:32, Krishnakant Mane yazmış:

Hello all,
Subject line says it all.
Basically what I want to do is to get last record from a result set.
I am dealing with a situation where given a date I need to know the last
record pertaining to transaction on a given account.
yes, it is an accounting/ book keeping software.
So I thought there was some thing like .last() method for a resultset?
Or even better do we have some thing like session.query(table).last()
The problem is that my logic is in place but I know that performance
wise it is very dirty to get the list of all records, just to loop till
the end and throw away all the rest of the rows.
So plese suggest how can I only get just that one (last) record?
Happy hacking.
Krishnakant.





--
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] how to get last record from a resultset

2011-07-20 Thread King Simon-NFHD78
If you don't have something consistent to sort by, then I'm not sure that the 
last record is meaningful, is it? If you have 10 rows with the same voucher 
code and account code (and there is nothing else to uniquely identify them, 
such as a more precise timestamp, or an auto-incrementing ID), then as far as 
the result set is concerned, there is nothing special about the last row. The 
database could be giving them to you in any order.

I'm sure I'm misunderstanding your situation - perhaps you could describe your 
schema and why you need this information, and then we might be more help.

Simon

 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Krishnakant Mane
 Sent: 20 July 2011 15:16
 To: sqlalchemy@googlegroups.com
 Cc: Timuçin Kızılay
 Subject: Re: [sqlalchemy] how to get last record from a resultset
 
 Well, there won't be a consistent result using sort because there
 might
 be 10 rows with same voucher code and same account code.
 That's exactly the challenge so I don't know how sort will help.
 If we can invert the entire resultset having the last record become
 first,  then its worth while.
 But again, I don't want the entire set of rows in the first place.
 I just want that particular row.
 Happy hacking.
 Krishnakant.
 
 On 20/07/11 19:20, Timuçin Kızılay wrote:
  I think, reversing the sort and getting the first record will do.
 
 
 
  20-07-2011 16:32, Krishnakant Mane yazmış:
  Hello all,
  Subject line says it all.
  Basically what I want to do is to get last record from a result
 set.
  I am dealing with a situation where given a date I need to know
 the last
  record pertaining to transaction on a given account.
  yes, it is an accounting/ book keeping software.
  So I thought there was some thing like .last() method for a
 resultset?
  Or even better do we have some thing like
 session.query(table).last()
  The problem is that my logic is in place but I know that
 performance
  wise it is very dirty to get the list of all records, just to loop
 till
  the end and throw away all the rest of the rows.
  So plese suggest how can I only get just that one (last) record?
  Happy hacking.
  Krishnakant.
 
 
 
 --
 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.



[sqlalchemy] Re: Occasional ProgrammingError in threaded environment

2011-07-20 Thread Arthur Kopatsy
Hi Michael,

I indeed had a object hanging around and being shared across
threads... Thanks for the hint, you saved my week!

Arthur

On Jul 20, 6:46 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Jul 20, 2011, at 1:50 AM, Arthur Kopatsy wrote:

  Hi,

  My web application has a controller that spawns multiple threads using
  a thread pool. Each of them has its own session (I store it in thread
  local). Occasionally (1 out of 100 or more)  I get really strange
  errors that really look like corrupted queries.

  My guess is that it may be due to my thread pooling and the fact that
  a session may be reused for multiple jobs.

 Yes this is most likely basic sharing of a Session and/or related state 
 between threads.  The stack trace specifically is the access of an unloaded 
 or expired attribute on a persistent object - this consults the Session to 
 refresh the object.    Accesses like these need to be kept local to a single 
 thread.

-- 
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] order_by with property of related table

2011-07-20 Thread gwozdziu
Hi!

I have two tables: A and B defined something like that:

A:
Column('id', Integer, primary_key=True),
Column('name', Unicode(256)),
Column('b_id', Integer, ForeignKey('b.id'))

B:
Column('id', Integer, primary_key=True),
Column('name', Unicode(256)),

mapper of A is created with:
properties={
'b': relation(B, primaryjoin=A.b_id == B.id, lazy=False),
}
(lazy = False is important in this case)

How can I select all elements from A sorted by B.name? I checked that
I can't use
SESSION.query(A).order_by(B.name)
because it's not working - query(A) consist of JOIN with B table and B
table has alias B_1 in this query and sqlalchemy interprets
order_by(B.name) as ORDER BY B.name but there is no B alias in
query(A).

-- 
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] py32 TypeError: unorderable types: str() int()

2011-07-20 Thread ddarko
declarative_base

tmp = session.query(Opt).all()
session.add(Opt(key='a', value='b'))
session.commit()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/session.py, line 617, in commit
self.transaction.commit()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/session.py, line 293, in commit
self._prepare_impl()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/session.py, line 277, in _prepare_impl
self.session.flush()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/session.py, line 1473, in flush
self._flush(objects)
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/session.py, line 1542, in _flush
flush_context.execute()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/unitofwork.py, line 327, in execute
rec.execute(self)
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/unitofwork.py, line 471, in execute
uow
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/mapper.py, line 1870, in _save_obj
for state in _sort_states(states):
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
py3.2.egg/sqlalchemy/orm/mapper.py, line 2808, in _sort_states
return sorted(states, key=operator.attrgetter('sort_key'))
TypeError: unorderable types: str()  int()

-- 
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] py32 TypeError: unorderable types: str() int()

2011-07-20 Thread Michael Bayer
Can I ask that you put a little more effort into providing context / mappings / 
specifics for this stack trace (see guidelines at 
http://www.sqlalchemy.org/support.html#mailinglist ) ?  Clearly If I make a 
sample Opt() class with two fields and commit it, no such error occurs.  There 
is some suggestion of inappropriate assignment of a string or int to an 
attribute which expects the opposite here.


On Jul 20, 2011, at 1:26 PM, ddarko wrote:

 declarative_base
 
 tmp = session.query(Opt).all()
 session.add(Opt(key='a', value='b'))
 session.commit()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/session.py, line 617, in commit
self.transaction.commit()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/session.py, line 293, in commit
self._prepare_impl()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/session.py, line 277, in _prepare_impl
self.session.flush()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/session.py, line 1473, in flush
self._flush(objects)
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/session.py, line 1542, in _flush
flush_context.execute()
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/unitofwork.py, line 327, in execute
rec.execute(self)
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/unitofwork.py, line 471, in execute
uow
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/mapper.py, line 1870, in _save_obj
for state in _sort_states(states):
  File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
 py3.2.egg/sqlalchemy/orm/mapper.py, line 2808, in _sort_states
return sorted(states, key=operator.attrgetter('sort_key'))
 TypeError: unorderable types: str()  int()
 
 -- 
 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.



[sqlalchemy] Re: py32 TypeError: unorderable types: str() int()

2011-07-20 Thread ddarko
That is very strange. I am from a few hours struggling with this ...
stripped application on the first ...
There is nothing else.
Ordinary simple table. On it a select and an insert (with ORM, of
course).

Rest a while and try to prepare a complete test case. Unless I find a
solution on the way :]


On Jul 20, 7:38 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Can I ask that you put a little more effort into providing context / mappings 
 / specifics for this stack trace (see guidelines 
 athttp://www.sqlalchemy.org/support.html#mailinglist) ?  Clearly If I make a 
 sample Opt() class with two fields and commit it, no such error occurs.  
 There is some suggestion of inappropriate assignment of a string or int to an 
 attribute which expects the opposite here.

 On Jul 20, 2011, at 1:26 PM, ddarko wrote:







  declarative_base

  tmp = session.query(Opt).all()
  session.add(Opt(key='a', value='b'))
  session.commit()
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/session.py, line 617, in commit
     self.transaction.commit()
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/session.py, line 293, in commit
     self._prepare_impl()
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/session.py, line 277, in _prepare_impl
     self.session.flush()
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/session.py, line 1473, in flush
     self._flush(objects)
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/session.py, line 1542, in _flush
     flush_context.execute()
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/unitofwork.py, line 327, in execute
     rec.execute(self)
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/unitofwork.py, line 471, in execute
     uow
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/mapper.py, line 1870, in _save_obj
     for state in _sort_states(states):
   File /usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.7.1-
  py3.2.egg/sqlalchemy/orm/mapper.py, line 2808, in _sort_states
     return sorted(states, key=operator.attrgetter('sort_key'))
  TypeError: unorderable types: str()  int()

  --
  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 
  athttp://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.