[sqlalchemy] Re: Updating field by applying transformation function to current field value

2010-06-10 Thread bartomas
Many thanks. Great help.

On Jun 9, 4:44 pm, Conor conor.edward.da...@gmail.com wrote:
 On 06/09/2010 07:58 AM, bartomas wrote:



  Hi,

  I'm new to SqlAlchemy. I'd like to find the simplest way of updating
  the fields of a table by applying a transformation function to the
  current value of the field.
  I've tried the following:

  ## Code snippet

      engine = create_engine('mysql://root:t...@localhost:3306/Test1')
      metadata = MetaData()
      metadata.bind = engine
      employees = Table('employees', metadata, autoload=True)

      upd =
  employees.update(values={employees.c.fullname:transform2(employees.c.fullname)})

      engine.execute(upd)

  def transform2(currentValue):
      return re.sub('Peter','Paul',currentValue)

  def transform1(currentValue):
      return 'Mr ' + currentValue

  ## end code snippet

  When applying the function transform1  which just concatenates another
  string to the current value, it works fine. However if I apply
  transform2 that does a regular expression substitution on the value I
  get an error message saying that the re.sub function expects a string
  argument.
  How can I retrieve the current value from a Column object to transform
  it? Or is there a better way to this problem?

  Many thanks for any help.

 In both transform functions, currentValue is the column object, not the
 value for a particular row. transform1 happens to work because column
 objects can turn the + operator into a SQL expression. To get the same
 effect for transform2, you have to use database functions, e.g. for
 PostgreSQL:

 upd = employees.update(values={employees.c.fullname: 
 func.regex_replace(employees.c.fullname, 'Peter', 'Paul')})

 If the transform function really does have to be run client-side, you
 have no choice but to SELECT all the rows, apply the transform, and
 issue many UPDATEs back to the database. Using the ORM can make this easier.

 -Conor

-- 
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] Some weir(for me) behavior of one-to-one relation.

2010-06-10 Thread bogun . dmitriy
 I have 2 tables with one-to-one relation, and I got some unexpected
 behaviour from sqlalchemy.
 In attach there is demonstration script and its log.

 If I try to add object into table right for already existing key I
 expect error on DB level, telling about violation of unique
 constraint, but sqlalchemy before try to insert new row, set relation
 key to NULL for already existing object...

 Is it right?
 How can I avoid such behavior?

 if you are replacing an object reference with another one, then yes SQLA will 
 null out the key for the old one first, since you have removed it from its 
 parent by replacing it.  If you dont want the NULL allowed, the usual 
 approach is to have the left_idnr column be NOT NULL - the database then does 
 the work of disallowing the operation to proceed.   There is a setting for 
 passive_deletes, 'all', which disallows the nulling out of the foreign key, 
 but that only applies to a cascading deletion scenario which is not the case 
 here.

 For one-to-ones I usually set the foreign key on the child as the primary key 
 as well.    You can see me asking about this (since a DBA gave me some 
 resistance about it recently) here: 
 http://stackoverflow.com/questions/2967450/foreign-key-constraints-on-primary-key-columns-issues

I try to change schema(updated example in attach, it drop
AssertionException), to use foreign key as primary key for child
table, but it doesn't want to work in this configuration at all.
Please give me link on sqlalchemy documentation, where I can read
about such usage of primary key.

There is only one place in sqlalchemy documentation where I see such
usage of primary key
http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html?highlight=declarative#joined-table-inheritance
but this is not my case.

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

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#import pdb

import os, sys
import time
import logging
#import re
#import errno
#import locale
#import pprint

# mutable inputs
from sqlalchemy import *
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

decl = declarative_base()

class left(decl):
__tablename__ = 'left'

idnr= Column(Integer, primary_key=True)
data= Column(String(32), nullable=False)

rel	= orm.relation('right', uselist=False,
	backref=orm.backref('left'),
	passive_updates=True, passive_deletes=True, lazy=True, cascade='all')

class right(decl):
__tablename__ = 'right'

#idnr= Column(Integer, primary_key=True)
left_idnr = Column(Integer, ForeignKey(left.idnr, onupdate='CASCADE', ondelete='CASCADE'), primary_key=True, unique=True)
data= Column(String(32), nullable=False)

def main():
logging.basicConfig()
log = logging.getLogger('sqlalchemy.engine')
log.setLevel(logging.DEBUG)

eng = create_engine('sqlite://')
sm = orm.sessionmaker(bind=eng, autoflush=False, autocommit=False)

decl.metadata.create_all(eng)
ses = sm()

l = left()
l.data = 'abc'
ses.add(l)

r = right()
r.left = l
r.data = 'cde'
ses.add(r)
ses.commit()
l_idnr = l.idnr
ses.close()

ses = sm()
l = ses.query(left).get(l_idnr)
r2 = right()
r2.left = l
r2.data = '012'
ses.flush()
ses.commit()

if __name__ == '__main__':
main()


[sqlalchemy] help please

2010-06-10 Thread Aref
Hello All,

I just began learning sqlalchemy and am not quite used to it yet so
please excuse my ignorance and which might be a trivial question to
some of you.
I am writing a database module and need to load a table and possibly
modify a record in the table. I can get the connection established and
everything works fine. The problem I am running into is that I do not
necessarily know the column name before hand to code it in the update
method. I want to be able to find out to send a generic column name
which will be updated (gets the column name dynamically).

I tried the following:

columns=['ProjectID', 'Program', 'progmanger']
test = str('table.c.'+columns[1])
update = table.update(test=='project-name', values = {test:'program'})
print update
update.execute()

I get a error when I try to run it. It does not recognize the column
for some reason even though if I print test everything seems to be OK.
I get 'project.c.Program'

Is there something I am missing here? How can I send the project and
column name to the update method dynamically?

Thank you so much in advance for any help or insight you could provide.

-- 
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] good IDE for SQLAlchemy and wxPython?

2010-06-10 Thread Carl Brewer

G'day,
I'm developing my first desktop application with python and a database 
(to track cycling track sprinters, if anyone's interested, using sqlite 
at the moment, but anything lightweight is fine) - and have been poking 
around various IDE's and database layers that work with Python, 
SQLAlchemy seems to be the neatest way to tie a class to a database 
without having to manually do all the database 'stuff' by using the ORM. 
If only this stuff existed back when I was at Uni and trying to work out 
how to save a C structure, wow .. but anyway .. !


I've found Pythoncard as an IDE and toolkit, but it seems a bit 
inactive, I'm developing on Windows XP 32 bit and am wondering what sort 
of IDE works well with SQLAlchemy and wxPython?  Should I just use the 
pythoncard editor (which works well enough as an editor but the whole 
setup of GUIs in template files is something I'm unsure about) or is 
there a better alternative?  I'm using wxGlade to mockup my interfaces 
and then copying the bits it generates into my program, but am wondering 
if there's any SQLAlchemy-aware IDE's that might speed up making forms 
and so on, rather than coding them all by hand?



--
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] Sybase reports conversion from datatype 'CHAR' to 'INT' is not allowed. when using string instead of int for filterby

2010-06-10 Thread Cserna, Zsolt
 
 
 SQLAlchemy doesn't do type coercion out of the box.  Some 
 DBAPIs do, which is why you may have not noticed this issue 
 on other platforms.   You can build that functionality 
 yourself using TypeDecorator:
 
 http://www.sqlalchemy.org/docs/reference/sqlalchemy/types.htm?highlight=typedecorator#sqlalchemy.types.TypeDecorator


I've fixed my problem with the TypeDecorator you suggested. Thanks.

Just to fix that, sqlalchemy doesn't do type coercion by default, and it will 
never do it. Is it correct?

In my test application I was able to replace the Integer type to the decorated 
one, which is fine, but requires the same 4-line code for my projects. It would 
be better if sqlalchemy would do it for me by default but if it doesn't fit to 
the design I can accept that.

Thanks,
Zsolt

--
NOTICE: If received in error, please destroy, and notify sender. Sender does 
not intend to waive confidentiality or privilege. Use of this email is 
prohibited when received in error. We may monitor and store emails to the 
extent permitted by applicable law.

-- 
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] Kerberos authentication with sybase

2010-06-10 Thread Cserna, Zsolt

Hi all,

Is there any plan to add kerberos authentication to the sybase engine in 
sqlalchemy?

I've implemented it but it's using the creator parameter of the create_engine 
function, which is ok, but in certain circumstances when the application using 
sqlalchemy uses configuration from a text file, I'm not able to do it easily 
(in pylons I need to modify my templates, or add a bunch of code to each 
project).

It would be nice if sqlalchemy would be able to accept the server's principal 
specified in the connection string somehow - and if it's specified use kerberos 
to authenticate the client.

Is it possible to implement this modification in sqlalchemy? I'm happy to 
contribute my current implementation.

Thanks,
Zsolt

--
NOTICE: If received in error, please destroy, and notify sender. Sender does 
not intend to waive confidentiality or privilege. Use of this email is 
prohibited when received in error. We may monitor and store emails to the 
extent permitted by applicable law.

-- 
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: help please

2010-06-10 Thread GHZ
you should access column names via lower case

i.e.

columns = 'projectid', 'program', 'progmanger']

On 10 Jun, 03:39, Aref arefnamm...@gmail.com wrote:
 Hello All,

 I just began learning sqlalchemy and am not quite used to it yet so
 please excuse my ignorance and which might be a trivial question to
 some of you.
 I am writing a database module and need to load a table and possibly
 modify a record in the table. I can get the connection established and
 everything works fine. The problem I am running into is that I do not
 necessarily know the column name before hand to code it in the update
 method. I want to be able to find out to send a generic column name
 which will be updated (gets the column name dynamically).

 I tried the following:

 columns=['ProjectID', 'Program', 'progmanger']
 test = str('table.c.'+columns[1])
 update = table.update(test=='project-name', values = {test:'program'})
 print update
 update.execute()

 I get a error when I try to run it. It does not recognize the column
 for some reason even though if I print test everything seems to be OK.
 I get 'project.c.Program'

 Is there something I am missing here? How can I send the project and
 column name to the update method dynamically?

 Thank you so much in advance for any help or insight you could provide.

-- 
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] good IDE for SQLAlchemy and wxPython?

2010-06-10 Thread werner

Carl,

On 10/06/2010 08:17, Carl Brewer wrote:

G'day,
I'm developing my first desktop application with python and a database 
(to track cycling track sprinters, if anyone's interested, using 
sqlite at the moment, but anything lightweight is fine) - and have 
been poking around various IDE's and database layers that work with 
Python, SQLAlchemy seems to be the neatest way to tie a class to a 
database without having to manually do all the database 'stuff' by 
using the ORM. If only this stuff existed back when I was at Uni and 
trying to work out how to save a C structure, wow .. but anyway .. !


I've found Pythoncard as an IDE and toolkit, but it seems a bit 
inactive, I'm developing on Windows XP 32 bit and am wondering what 
sort of IDE works well with SQLAlchemy and wxPython?  Should I just 
use the pythoncard editor (which works well enough as an editor but 
the whole setup of GUIs in template files is something I'm unsure 
about) or is there a better alternative?  I'm using wxGlade to mockup 
my interfaces and then copying the bits it generates into my program, 
but am wondering if there's any SQLAlchemy-aware IDE's that might 
speed up making forms and so on, rather than coding them all by hand?

That would be nice but I am not aware of one.

Personally I use Boa to develop, it has a GUI designer built in you can 
use to generate the dialogs/frames etc and I then use validators to load 
data to/from the controls.


Dabo (http://www.dabodev.com/) might also be an alternative you want to 
look at, also it has is own db access stuff - I asked in the past if 
they don't want to switch to SA but there doesn't seem to be any 
interest in doing this.


Werner








--
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] Having case problem with column name in postgresql

2010-06-10 Thread Michael Bayer

On Jun 10, 2010, at 12:06 AM, Mike Bernson wrote:

 I am trying to access a postgres database with mixed case column names.
 
 I am using reflection to get the table.
 
 Here is my connect string:
 'postgresql://:mike@/cedar_senior_services'
 
 
 The error message:
 (ProgrammingError) column active does not exist
 LINE 1: select * from license where Active=E'yes' and LicenseType=E'...
^
 'select * from license where Active=%(Active)s and 
 LicenseType=%(LicenseType)s' {'Active': 'yes', 'LicenseType': 'ADMIN'}
 
 The error message is correct that active does not exist but Active is the 
 requested column.
 
 How do I get sqlalchemy when using postgres to quote thing so that looks for
 same case column names.
 
 Using Sqlalchemy 6.1 with postgres 8.4.4
 
 The follow selection does work:
 select * from license where Active='yes' and LicenseType='ADMIN';
 
 Since this is my first try as using postgres from sqlalchemy I am sure that
 I must by missing something simple on the connection url or create engine.

any column name with capital letters in it will be quoted in all cases.   If 
you are using table metadata and its spitting out Active without quotes, 
thats a huge bug, although above that looks like a hand-coded statement.   

Using a table with a column named Active I get:

SELECT t1.Active FROM t1






-- 
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] Sybase reports conversion from datatype 'CHAR' to 'INT' is not allowed. when using string instead of int for filterby

2010-06-10 Thread Michael Bayer

On Jun 10, 2010, at 8:51 AM, Cserna, Zsolt wrote:

 
 
 SQLAlchemy doesn't do type coercion out of the box.  Some 
 DBAPIs do, which is why you may have not noticed this issue 
 on other platforms.   You can build that functionality 
 yourself using TypeDecorator:
 
 http://www.sqlalchemy.org/docs/reference/sqlalchemy/types.htm?highlight=typedecorator#sqlalchemy.types.TypeDecorator
 
 
 I've fixed my problem with the TypeDecorator you suggested. Thanks.
 
 Just to fix that, sqlalchemy doesn't do type coercion by default, and it will 
 never do it. Is it correct?
 
 In my test application I was able to replace the Integer type to the 
 decorated one, which is fine, but requires the same 4-line code for my 
 projects. It would be better if sqlalchemy would do it for me by default but 
 if it doesn't fit to the design I can accept that.

from string- something is not always a clean cut affair (though it is for 
int()) , but it also adds overhead to the usage of bind parameters, which can 
add up if you're doing an executemany() style call with thousands of parameter 
sets. Its better that this additional behavior is left up to extensions.

Any non-trivial SQLAlchemy project would usually have a util.py of some sort 
that contains a set of SQLAlchemy idioms for usage throughout the application.  
 In mine I usually have a whole set of types that are specific to the use case 
at hand, column-generating macros that enforce naming schemes, stuff like that. 
  

-- 
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] Kerberos authentication with sybase

2010-06-10 Thread Michael Bayer
if you want to show me how that's done with your DBAPI (what DBAPI is this?), 
sure, though if its something that you pass in via **kw to DBAPI.connect(), you 
can already do that by passing connect_args to create_engine(), or adding them 
onto the querystring db://...?foo=bar.



On Jun 10, 2010, at 9:10 AM, Cserna, Zsolt wrote:

 
 Hi all,
 
 Is there any plan to add kerberos authentication to the sybase engine in 
 sqlalchemy?
 
 I've implemented it but it's using the creator parameter of the create_engine 
 function, which is ok, but in certain circumstances when the application 
 using sqlalchemy uses configuration from a text file, I'm not able to do it 
 easily (in pylons I need to modify my templates, or add a bunch of code to 
 each project).
 
 It would be nice if sqlalchemy would be able to accept the server's principal 
 specified in the connection string somehow - and if it's specified use 
 kerberos to authenticate the client.
 
 Is it possible to implement this modification in sqlalchemy? I'm happy to 
 contribute my current implementation.
 
 Thanks,
 Zsolt
 
 --
 NOTICE: If received in error, please destroy, and notify sender. Sender does 
 not intend to waive confidentiality or privilege. Use of this email is 
 prohibited when received in error. We may monitor and store emails to the 
 extent permitted by applicable law.
 
 -- 
 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.



[sqlalchemy] Re: help please

2010-06-10 Thread Aref
Thank you for the response. However, that is not the problem. If I do

update = table.update(project.c.ProjectID=='project-name', values =
{project.c.ProjectID:'program'})
print update
update.execute()

everything works fine.
if I do this:

test = 'table.c.'+columns[0] #columns is a list which contains the
columns names
update = table.update(test == 'project-name', values={test:'program'})
update.execute()

it does not work. I get an error that there is no such column.
I need to be able to update columns dynamically where I do not have a
prior knowledge of what tables and what are the table columns that may
exist. How can I do that if at all?

On Jun 10, 7:21 am, GHZ geraint.willi...@gmail.com wrote:
 you should access column names via lower case

 i.e.

 columns = 'projectid', 'program', 'progmanger']

 On 10 Jun, 03:39, Aref arefnamm...@gmail.com wrote:



  Hello All,

  I just began learning sqlalchemy and am not quite used to it yet so
  please excuse my ignorance and which might be a trivial question to
  some of you.
  I am writing a database module and need to load a table and possibly
  modify a record in the table. I can get the connection established and
  everything works fine. The problem I am running into is that I do not
  necessarily know the column name before hand to code it in the update
  method. I want to be able to find out to send a generic column name
  which will be updated (gets the column name dynamically).

  I tried the following:

  columns=['ProjectID', 'Program', 'progmanger']
  test = str('table.c.'+columns[1])
  update = table.update(test=='project-name', values = {test:'program'})
  print update
  update.execute()

  I get a error when I try to run it. It does not recognize the column
  for some reason even though if I print test everything seems to be OK.
  I get 'project.c.Program'

  Is there something I am missing here? How can I send the project and
  column name to the update method dynamically?

  Thank you so much in advance for any help or insight you could provide.- 
  Hide quoted text -

 - Show quoted text -

-- 
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] help please

2010-06-10 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of Aref
 Sent: 10 June 2010 02:40
 To: sqlalchemy
 Subject: [sqlalchemy] help please
 
 Hello All,
 
 I just began learning sqlalchemy and am not quite used to it yet so
 please excuse my ignorance and which might be a trivial question to
 some of you.
 I am writing a database module and need to load a table and possibly
 modify a record in the table. I can get the connection established and
 everything works fine. The problem I am running into is that I do not
 necessarily know the column name before hand to code it in the update
 method. I want to be able to find out to send a generic column name
 which will be updated (gets the column name dynamically).
 
 I tried the following:
 
 columns=['ProjectID', 'Program', 'progmanger']
 test = str('table.c.'+columns[1])
 update = table.update(test=='project-name', values = {test:'program'})
 print update
 update.execute()
 
 I get a error when I try to run it. It does not recognize the column
 for some reason even though if I print test everything seems to be OK.
 I get 'project.c.Program'
 
 Is there something I am missing here? How can I send the project and
 column name to the update method dynamically?
 
 Thank you so much in advance for any help or insight you 
 could provide.
 

The table.c object supports dictionary-style access, so you should be
able to use something like this:

  colname = 'Program'
  column = table.c[colname]
  update = table.update(column=='project-name', values =
{test:'program'})

However, in general, if you want to get a named attribute of an object,
and the name is stored in a variable, you can use Python's getattr
function. This code should also work:

  colname = 'Program'
  column = getattr(table.c, colname)
  update = table.update(column=='project-name', values =
{test:'program'})

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.



Re: [sqlalchemy] Re: help please

2010-06-10 Thread Lance Edgar




On 6/10/2010 10:29 AM, Aref wrote:

  Thank you for the response. However, that is not the problem. If I do

update = table.update(project.c.ProjectID=='project-name', values =
{project.c.ProjectID:'program'})
print update
update.execute()

everything works fine.
if I do this:

test = 'table.c.'+columns[0] #columns is a list which contains the
columns names
update = table.update(test == 'project-name', values={test:'program'})
update.execute()

it does not work. I get an error that there is no such column.
I need to be able to update columns dynamically where I do not have a
prior knowledge of what tables and what are the table columns that may
exist. How can I do that if at all?
  


Instead try:

update = table.update(eval(test)=='project-name',
values={test:'program'})

I can't say for sure that's the best way to do it still, but it
would solve your immediate problem. The "test" variable is referencing
a string, not a column. You have to eval() it to get the column
reference.

However, you say your error is that "there is no such column" ... I'd
expect a much different error if my suggestion were to actually fix
your problem. Anyway good luck. :) Might include your traceback next
time if you still have problems.

Lance





-- 
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] Having case problem with column name in postgresql

2010-06-10 Thread Mike Bernson



Michael Bayer wrote:

On Jun 10, 2010, at 12:06 AM, Mike Bernson wrote:


I am trying to access a postgres database with mixed case column names.

I am using reflection to get the table.

Here is my connect string:
'postgresql://:mike@/cedar_senior_services'


The error message:
(ProgrammingError) column active does not exist
LINE 1: select * from license where Active=E'yes' and LicenseType=E'...
   ^
'select * from license where Active=%(Active)s and LicenseType=%(LicenseType)s' 
{'Active': 'yes', 'LicenseType': 'ADMIN'}

The error message is correct that active does not exist but Active is the 
requested column.

How do I get sqlalchemy when using postgres to quote thing so that looks for
same case column names.

Using Sqlalchemy 6.1 with postgres 8.4.4

The follow selection does work:
select * from license where Active='yes' and LicenseType='ADMIN';

Since this is my first try as using postgres from sqlalchemy I am sure that
I must by missing something simple on the connection url or create engine.


any column name with capital letters in it will be quoted in all cases.   If you are using table metadata and its spitting out Active without quotes, thats a huge bug, although above that looks like a hand-coded statement.   


Using a table with a column named Active I get:

SELECT t1.Active FROM t1







I did some more hunting based on reply and did find that the query where
clause is coming from text strings and not metadata.

Thanks for the quick reply.

--
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: help please

2010-06-10 Thread Aref
Thank you very much. I'll try it. Is there a better way of doing this--
I mean there must be since this is necessary for any application
needing to modify a database where generally tables are accessed
dynamically.

On Jun 10, 9:37 am, Lance Edgar lance.ed...@gmail.com wrote:
 On 6/10/2010 10:29 AM, Aref wrote:Thank you for the response. However, that 
 is not the problem. If I do update = 
 table.update(project.c.ProjectID=='project-name', values = 
 {project.c.ProjectID:'program'}) print update update.execute() everything 
 works fine. if I do this: test = 'table.c.'+columns[0] #columns is a list 
 which contains the columns names update = table.update(test == 
 'project-name', values={test:'program'}) update.execute() it does not work. I 
 get an error that there is no such column. I need to be able to update 
 columns dynamically where I do not have a prior knowledge of what tables and 
 what are the table columns that may exist. How can I do that if at all?
 Instead try:update = table.update(eval(test)=='project-name', 
 values={test:'program'})I can't say for sure that's the best way to do it 
 still, but it would solve your immediate problem.  The test variable is 
 referencing a string, not a column.  You have to eval() it to get the column 
 reference.
 However, you say your error is that there is no such column ... I'd expect 
 a much different error if my suggestion were to actually fix your problem.  
 Anyway good luck. :)  Might include your traceback next time if you still 
 have problems.
 Lance

-- 
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] mssql 2000 Date type on sa 0.6 maps to non-existant DATE type

2010-06-10 Thread Clovis Fabricio
2010/6/9 Clovis Fabricio nos...@gmail.com:
 2010/6/9 Michael Bayer mike...@zzzcomputing.com:
 I think I know what the problem is.  You need to use freetds version 7.0 or 
 8.0.  If I set mine down to 4.2, I get your goofy results.
 I'm away from the server right now, I'll do more tests tomorrow, and
 send you the results.

ok, I did some testing and setting tds version to 8.0 fixed it.

As a side issue, I had tds version = 8.0 on my freetds.conf file,
but that is just not being read. When I use either the connection
string or the environment variable to set the tds version, it works.

I'll join freetds mailing list to check on this.

 I'm willing to open tickets and test more results, I'd love to see
 this solved for good.

Any suggestions on the next step?

--
nosklo

-- 
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: help please

2010-06-10 Thread Lance Edgar




On 6/10/2010 11:22 AM, Aref wrote:

  Thank you very much. I'll try it. Is there a better way of doing this--
I mean there must be since this is necessary for any application
needing to modify a database where generally tables are accessed
dynamically.
  


Well, I suspect the "better way" would be to follow King Simon's advice
(http://groups.google.com/group/sqlalchemy/msg/b8c0f6bce263ff3d?hl=en)
and avoid the eval() stuff as well. Instead you'd be using getattr()
or accessing the table.c collection dictionary-style.

Lance





-- 
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: Questions about session

2010-06-10 Thread Az
The pprintout was:

{type 'collections.defaultdict': 156,
 type 'bool': 2,
 type 'float': 1,
 type 'int': 538,
 type 'list': 1130,
 type 'dict': 867,
 type 'NoneType': 1,
 type 'set': 932,
 type 'str': 577,
 type 'tuple': 1717,
 type 'type': 5,
 class 'sqlalchemy.util.symbol': 1,
 class 'sqlalchemy.orm.state.InstanceState': 236,
 class 'ProjectParties.Student': 156,
 class 'ProjectParties.Supervisor': 39,
 class 'ProjectParties.Project': 197}

I think the InstanceStates come from the Supervisor and Project
classes (197+39 = 236)

 Sounds pretty ugly. What if you add extra tables to represent runs
 and/or trials?

 class Run(Base):
 # Having a separate table here gives you nice auto-incrementing run ids
 # and lets you attach additional information to a run, such as timestamp,
 # human-supplied comment, etc.
 __tablename__ = 'run'
 id = Column(Integer, primary_key=True)
 timestamp = Column(DateTime, nullable=False)
 # comment = Column(UnicodeText(100), nullable=False)

 trials = relationship('Trial',
   back_populates='run',
   order_by=lambda: Trial.id.asc())

 class Trial(Base):
 # Having a separate table here is of dubious value, but hey it makes the
 # relationships a bit nicer!
 __tablename__ = 'trial'
 __table_args__ = (PrimaryKeyConstraint('run_id', 'id'), {})
 run_id = Column(Integer, ForeignKey('run.id'))
 id = Column(Integer)

 run = relationship('Run', back_populates='trials')
 sim_allocs = relationship('SimAllocation', back_populates='trial')

 class SimAllocation(Base):
 ...
 __table_args__ = (PrimaryKeyConstraint('run_id', 'trial_id', 'stud_id'),
   ForeignKeyConstraint(['run_id', 'trial_id'],
['trial.run_id', 'trial.id']),
   {})

 run_id = Column(Integer)
 trial_id = Column(Integer)
 stud_id = Column(Integer)

 trial = relationship('Trial', back_populates='sim_allocs')

Ah true, my solution was rather hacky and not very elegant.

Your class definitions... are you defining both table and Class in one
go? Would I have to change the way my monteCarloBasic creates
instances of SimAllocation?



On Jun 9, 9:46 pm, Conor conor.edward.da...@gmail.com wrote:
 On 06/09/2010 02:45 PM, Az wrote:



  Expected: students, supervisors, projects, dictionaries of said objects,
  and other attribute values (strings, ints, lists, etc.). Unexpected:
  anything else, especially sessions, InstanceState objects, or other ORM
  support objects.

  Actually got some stuff like the following (copy-pasting bits from my
  print output):

  (class 'sqlalchemy.orm.state.InstanceState',)
  {'_sa_instance_state': sqlalchemy.orm.state.InstanceState object at
  0x2d5beb0, 'proj_id': 1100034, 'postsim_probs': [], 'proj_sup': 1291,
  'presim_pop': 0, 'own_project': False, 'allocated': False,
  'proj_name': 'MPC on a Chip', 'blocked': False}

  Stuff like that :S

 I'm not sure what that printout indicates. Try this as your debug printout:

 def get_memo_type_count(memo):
     retval = {}
     for obj in memo.itervalues():
         type_ = obj.__class__
         retval[type_] = retval.get(type_, 0) + 1
     return retval

 [perform deep copies]
 type_count = get_memo_type_count(memo)
 import pprint
 pprint.pprint(type_count)

 This will tell you, e.g. how may Student objects were copied, how many
 InstanceState objects were copied, etc. Remember that you will have to
 override __deepcopy__ on your mapped classes or use the
 use-case-specific copy function to prevent ORM attributes (such as
 _sa_instance_state) from being copied.



  [...]
  The most likely cause is if you call session.add(temp_alloc) after
  calling session.merge(temp_alloc) for the same temp_alloc object. I
  noticed your original monteCarloBasic had two calls to
  session.add(temp_alloc); did both get changed to
  session.merge(temp_alloc)? If that doesn't work, can you verify that
  SQLAlchemy's primary key for SimAllocation matches the database's
  primary key for sim_alloc? What column type are you using for uid? Which
  call to session.merge is failing (line 163 according to your traceback),
  the one inside your for rank in ranks loop or the one outside?

  Oh yeah good point, they're separate calls. Basically for the one in
  for rank in ranks
  adds for a student getting a project, the other adds if a student
  doesn't get a project since we want
  to track all students (allocated or not, since the state of being
  unallocated is what gives
  us motivation to optimise the results).

 Your original monteCarloBasic definition had this:

 for rank in ranks:
     proj = random.choice(list(student.preferences[rank]))
     if not (proj.allocated or proj.blocked or proj.own_project):
         [...]
         session.add(temp_alloc) # #1
         break

 ident += 1
 session.add(temp_alloc) # #2

 session.add #1 is redundant since #2 gets 

[sqlalchemy] Re: connection lost contact error for first database connection every morning

2010-06-10 Thread Kerrb
Thank you, we will try recycling every hour and see if tomorrow
connects without a hitch.

On Jun 9, 5:43 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 since nobody is getting back to you on this one you want to look into 
 pool_recycle 
 athttp://www.sqlalchemy.org/docs/reference/sqlalchemy/connections.html#

 On Jun 9, 2010, at 10:33 AM, Kerrb wrote:



  We are using SQLAlchemy with our Plone CMS to connect to an Oracle
  database. The connection pool is set to 5 and it works fine all day.
  The first person to try to use it in the morning gets a connection
  lost contact error. If they refresh they get the connection. Oracle
  is not dropping the connections - they show up in Oracle as inactive.
  Is there a switch we're missing when we set up the connection pool in
  SQLAlchemy?

  --
  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 
  athttp://groups.google.com/group/sqlalchemy?hl=en.- Hide quoted text -

 - Show quoted text -

-- 
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: Questions about session

2010-06-10 Thread Az
So I laid them out like this:

class Run(Base):
# For autoincrementing run IDs
# Allows addition of more information to a run
__tablename__ = 'run'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, nullable=False)
# comment = Column(UnicodeText(100), nullable=False)

trials = relationship('Trial',
  back_populates='run',
  order_by=lambda: Trial.id.asc())

class Trial(Base):
# Having a separate table here is of dubious value, but hey it
makes the
# relationships a bit nicer!
__tablename__ = 'trial'
__table_args__ = (PrimaryKeyConstraint('run_id', 'id'), {})
run_id = Column(Integer, ForeignKey('run.id'))
id = Column(Integer)

run = relationship('Run', back_populates='trials')
sim_allocs = relationship('SimAllocation', back_populates='trial')

class SimAllocation(Base):
#
__tablename__ = 'sim_alloc'
__table_args__ = (PrimaryKeyConstraint('run_id', 'trial_id',
'stud_id'),
 ForeignKeyConstraint(['run_id', 'trial_id'],
  ['trial.run_id',
'trial.id']),
 {})

   run_id = Column(Integer)
   trial_id = Column(Integer)
   stud_id = Column(Integer)

   trial = relationship('Trial', back_populates='sim_allocs')


def __init__(self, ident, uid, session_id, stud_id, alloc_proj_rank):
self.ident = ident
self.uid = uid
self.session_id = session_id
self.stud_id = stud_id
self.alloc_proj = None
self.alloc_proj_ref = None
self.alloc_proj_rank = alloc_proj_rank

def __repr__(self):
return str(self)

def __str__(self):
return Row: %s UID: %s - %s: Student: %s (Project: %s - Rank: 
%s) %
(self.ident, self.uid, self.session_id, self.stud_id, self.alloc_proj,
self.alloc_proj_rank)

#

The original mapping was:

simulation_allocation = Table('sim_alloc', metadata,
Column('ident', Integer),
Column('uid', String, primary_key=True),
Column('session_id', Integer, primary_key=True),
Column('stud_id', Integer, ForeignKey('studs.ee_id'),
primary_key=True),
Column('alloc_proj', Integer, ForeignKey('projs.proj_id')),
Column('alloc_proj_rank', Integer)
)

mapper(SimAllocation, simulation_allocation, properties={'stud' :
relation(StudentDBRecord), 'proj' : relation(Project)})

Of course, I'd get rid of the project relationship since an
allocated_project and allocated_proj_ref *can* be a NoneType
(realised that right now!)

Additionally, I'd like to maintain the ForeignKey relationship with
the StudentDRRecord table for pulling in info about a student.

Also, I've not got rid of ident because I don't know how else to map
SimAllocation to a dictionary as well. The only thing I could use for
keys was the IDENT before but now that we have a composite key, what
happens to the dictionary? However, the dictionary will just hold
information for the current run really.



On Jun 10, 6:33 pm, Az azfarul.is...@gmail.com wrote:
 The pprintout was:

 {type 'collections.defaultdict': 156,
  type 'bool': 2,
  type 'float': 1,
  type 'int': 538,
  type 'list': 1130,
  type 'dict': 867,
  type 'NoneType': 1,
  type 'set': 932,
  type 'str': 577,
  type 'tuple': 1717,
  type 'type': 5,
  class 'sqlalchemy.util.symbol': 1,
  class 'sqlalchemy.orm.state.InstanceState': 236,
  class 'ProjectParties.Student': 156,
  class 'ProjectParties.Supervisor': 39,
  class 'ProjectParties.Project': 197}

 I think the InstanceStates come from the Supervisor and Project
 classes (197+39 = 236)



  Sounds pretty ugly. What if you add extra tables to represent runs
  and/or trials?

  class Run(Base):
      # Having a separate table here gives you nice auto-incrementing run ids
      # and lets you attach additional information to a run, such as 
  timestamp,
      # human-supplied comment, etc.
      __tablename__ = 'run'
      id = Column(Integer, primary_key=True)
      timestamp = Column(DateTime, nullable=False)
      # comment = Column(UnicodeText(100), nullable=False)

      trials = relationship('Trial',
                            back_populates='run',
                            order_by=lambda: Trial.id.asc())

  class Trial(Base):
      # Having a separate table here is of dubious value, but hey it makes the
      # relationships a bit nicer!
      __tablename__ = 'trial'
      __table_args__ = (PrimaryKeyConstraint('run_id', 'id'), {})
      run_id = Column(Integer, ForeignKey('run.id'))
      id = Column(Integer)

      run = relationship('Run', back_populates='trials')
      sim_allocs = relationship('SimAllocation', back_populates='trial')

  class SimAllocation(Base):
      ...
      __table_args__ = (PrimaryKeyConstraint('run_id', 'trial_id', 'stud_id'),
                        

[sqlalchemy] mod_wsgi, SQLite3 and a threading anomaly because connection is not returned to pool by session.remove()

2010-06-10 Thread Vinay Sajip
I'm getting the SQLite objects created in a thread can only be used
in that same thread. error when using a web application with mod_wsgi
configured in daemon mode with processes=1, threads=15 on Ubuntu
Karmic, using Python2.6.

I saw another thread [1] which mentioned a similar issue, and I
believe I am using the approach recommended by Mike Bayer in that
thread.

I have a multi-db setup - 2 of the databases are SQLite and one is
PostgreSQL. There are three sessions, set up using

session_var = scoped_session(sessionmaker(extension=extn))

where the extension just allows listeners to be registered which are
called at before_flush and before_commit - I don't believe that's part
of the problem.

When a GET request is received, a user is retrieved from an ID stored
in the HTTP session - this uses a query attribute on the mapped class
which is obtained using session_var.query_property(). All conventional
stuff.

At the end of the request, I call

session_var.remove()

for each of the three sessions. This is what I understand to be the
correct practice - no instantiation of session_var is required.

I enabled debug logging for sqlalchemy.pool and find that under the
failing scenariio, the session_var.remove() sometimes fails to remove
the session. This leads to the SQLite error on a subsequent request.

The failing scenario: Invoke the same URL multiple times from Firefox,
setting a new locale preference (Accept-Language) between requests.
Pretty soon you get the error, full log file is here [2]. I modified
the pool.py code to display a stack trace in the log, so that I could
see what led to a connection being created. From what I can see, it's
the same code path each time.

It's a 219-line trace, but I'll reproduce the relevant lines here: The
PID and thread ID are in braces in each log message.

[ersatz.core:705]{5648/-1385407632} DEBUG dispatching request GET
http://localhost/admin/book/add/
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:275]{5648/-1385407632}
DEBUG Created new connection sqlite3.Connection object at 0x21d61ca0
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:282]{5648/-1385407632}
DEBUG Stack at this point snipped
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:337]{5648/-1385407632}
DEBUG Connection sqlite3.Connection object at 0x21d61ca0 checked out
from pool
[ersatz.core:746]{5648/-1385407632} DEBUG 0.420: time for
http://localhost/admin/book/add/ (end of request processing)
[ersatz.db:328]{5648/-1385407632} DEBUG session cleanup starting
[ersatz.db:336]{5648/-1385407632} DEBUG Calling session.remove() for
db1 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b6190c)
[ersatz.db:336]{5648/-1385407632} DEBUG Calling session.remove() for
db3 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b61aec)
[ersatz.db:336]{5648/-1385407632} DEBUG Calling session.remove() for
db2 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b61ccc)
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:310]{5648/-1385407632}
DEBUG Connection sqlite3.Connection object at 0x21d61ca0 being
returned to pool
[ersatz.db:338]{5648/-1385407632} DEBUG session cleanup done

As can be seen, the session.remove() for the session variable for db2
returns the connection to the pool, as expected. Next request:

[ersatz.core:705]{5648/-1238549648} DEBUG dispatching request GET
http://localhost/admin/book/add/
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:275]{5648/-1238549648}
DEBUG Created new connection sqlite3.Connection object at 0x2202f5a0
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:282]{5648/-1238549648}
DEBUG Stack at this point: snipped

The call to create this connection was made with the same code path as
for the previous request.

[sqlalchemy.pool.SingletonThreadPool.0x...c44c:337]{5648/-1238549648}
DEBUG Connection sqlite3.Connection object at 0x2202f5a0 checked out
from pool
[ersatz.core:746]{5648/-1238549648} DEBUG 0.085: time for
http://localhost/admin/book/add/ (end of request processing)
[ersatz.db:328]{5648/-1238549648} DEBUG session cleanup starting
[ersatz.db:336]{5648/-1238549648} DEBUG Calling session.remove() for
db1 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b6190c)
[ersatz.db:336]{5648/-1238549648} DEBUG Calling session.remove() for
db3 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b61aec)
[ersatz.db:336]{5648/-1238549648} DEBUG Calling session.remove() for
db2 (sqlalchemy.orm.scoping.ScopedSession object at 0x21b61ccc)
[ersatz.db:338]{5648/-1238549648} DEBUG session cleanup done

No connection was returned to the pool! Next request:

[ersatz.core:705]{5648/-1249039504} DEBUG dispatching request GET
http://localhost/admin/book/add/
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:275]{5648/-1249039504}
DEBUG Created new connection sqlite3.Connection object at 0x2202f4a0
[sqlalchemy.pool.SingletonThreadPool.0x...c44c:282]{5648/-1249039504}
DEBUG Stack at this point: snipped

Called from the same place as the previous two requests.


Re: [sqlalchemy] help with 0.6 migration

2010-06-10 Thread Michael Bayer
thats very interesting.  Would you care to share with us the error ?


On Jun 10, 2010, at 1:08 PM, Alexander Zhabotinskiy wrote:

 I'v get an error with this construction with migrating to 0.6.1
 
 
sList = []
if len(curDeps)  0:
for item in curDeps:
sList.append(Orderitem.serivcecomments.like('%:' +
 str(item) + '%'))
 
  .
.filter(or_( Orderitem.department_id.in_(curDeps),
 (and_((or_(*sList)), Orderitem.isbtype==True)) )) \
  
 
 -- 
 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.



[sqlalchemy] Re: Questions about session

2010-06-10 Thread Az
Let me take a guess:

class Supervisor(object):
def __init__(self, ee_id, name, original_quota, loading_limit):
self.ee_id = ee_id
self.name = name
self.original_quota = original_quota
self.loading_limit = loading_limit
self.predecr_quota = 0
self.offered_proj = set()
self.total_prealloc_pop = 0
self.total_postalloc_pop = 0

def __repr__(self):
return str(self)

def __str__(self):
return self.name
return %s %s %s (Offered projects: %s) %(self.ee_id, 
self.name,
self.predecr_quota, self.offered_proj)

So *inside* the Supervisor class would I define it like this (trying
to have a go at it)?

def __deepcopy__(self, memo):
dc = type(self)()
dc.__dict__.update(self.__dict__)
for attr in dir(supervisor):
if not attr.startswight('__'):
self.attr = deepcopy(self.attr, memo)

So this only overrides __deepcopy__ when I call it for a Supervisor
and not for any of the other classes right?



On Jun 10, 6:56 pm, Az azfarul.is...@gmail.com wrote:
 So I laid them out like this:

 class Run(Base):
     # For autoincrementing run IDs
         # Allows addition of more information to a run
     __tablename__ = 'run'
     id = Column(Integer, primary_key=True)
     timestamp = Column(DateTime, nullable=False)
     # comment = Column(UnicodeText(100), nullable=False)

     trials = relationship('Trial',
                           back_populates='run',
                           order_by=lambda: Trial.id.asc())

 class Trial(Base):
     # Having a separate table here is of dubious value, but hey it
 makes the
     # relationships a bit nicer!
     __tablename__ = 'trial'
     __table_args__ = (PrimaryKeyConstraint('run_id', 'id'), {})
     run_id = Column(Integer, ForeignKey('run.id'))
     id = Column(Integer)

     run = relationship('Run', back_populates='trials')
     sim_allocs = relationship('SimAllocation', back_populates='trial')

 class SimAllocation(Base):
 #
         __tablename__ = 'sim_alloc'
         __table_args__ = (PrimaryKeyConstraint('run_id', 'trial_id',
 'stud_id'),
                              ForeignKeyConstraint(['run_id', 'trial_id'],
                                                   ['trial.run_id',
 'trial.id']),
                              {})

    run_id = Column(Integer)
    trial_id = Column(Integer)
    stud_id = Column(Integer)

    trial = relationship('Trial', back_populates='sim_allocs')

         def __init__(self, ident, uid, session_id, stud_id, alloc_proj_rank):
                 self.ident = ident
                 self.uid = uid
                 self.session_id = session_id
                 self.stud_id = stud_id
                 self.alloc_proj = None
                 self.alloc_proj_ref = None
                 self.alloc_proj_rank = alloc_proj_rank

         def __repr__(self):
                 return str(self)

         def __str__(self):
                 return Row: %s UID: %s - %s: Student: %s (Project: %s - 
 Rank: %s) %
 (self.ident, self.uid, self.session_id, self.stud_id, self.alloc_proj,
 self.alloc_proj_rank)

 #

 The original mapping was:

 simulation_allocation = Table('sim_alloc', metadata,
         Column('ident', Integer),
         Column('uid', String, primary_key=True),
         Column('session_id', Integer, primary_key=True),
         Column('stud_id', Integer, ForeignKey('studs.ee_id'),
 primary_key=True),
         Column('alloc_proj', Integer, ForeignKey('projs.proj_id')),
         Column('alloc_proj_rank', Integer)
 )

 mapper(SimAllocation, simulation_allocation, properties={'stud' :
 relation(StudentDBRecord), 'proj' : relation(Project)})

 Of course, I'd get rid of the project relationship since an
 allocated_project and allocated_proj_ref *can* be a NoneType
 (realised that right now!)

 Additionally, I'd like to maintain the ForeignKey relationship with
 the StudentDRRecord table for pulling in info about a student.

 Also, I've not got rid of ident because I don't know how else to map
 SimAllocation to a dictionary as well. The only thing I could use for
 keys was the IDENT before but now that we have a composite key, what
 happens to the dictionary? However, the dictionary will just hold
 information for the current run really.

 On Jun 10, 6:33 pm, Az azfarul.is...@gmail.com wrote:

  The pprintout was:

  {type 'collections.defaultdict': 156,
   type 'bool': 2,
   type 'float': 1,
   type 'int': 538,
   type 'list': 1130,
   type 'dict': 867,
   type 'NoneType': 1,
   type 'set': 932,
   type 'str': 577,
   type 'tuple': 1717,
   type 'type': 5,
   class 'sqlalchemy.util.symbol': 1,
   class 'sqlalchemy.orm.state.InstanceState': 236,
   class 'ProjectParties.Student': 156,
   class 'ProjectParties.Supervisor': 39,
   class 'ProjectParties.Project': 197}

  I think the InstanceStates come from the Supervisor and 

Re: [sqlalchemy] mod_wsgi, SQLite3 and a threading anomaly because connection is not returned to pool by session.remove()

2010-06-10 Thread Michael Bayer

On Jun 10, 2010, at 2:54 PM, Vinay Sajip wrote:

 I'm getting the SQLite objects created in a thread can only be used
 in that same thread. error when using a web application with mod_wsgi
 configured in daemon mode with processes=1, threads=15 on Ubuntu
 Karmic, using Python2.6.


 
 When a GET request is received, a user is retrieved from an ID stored
 in the HTTP session - this uses a query attribute on the mapped class
 which is obtained using session_var.query_property(). All conventional
 stuff.

First off, the absolute recommended behavior for SQLite if a file-based 
database is to not use pooling.  I would suggest you use a NullPool to 
eliminate any connection pooling.  Some detail on this at 
http://www.sqlalchemy.org/docs/reference/dialects/sqlite.html#threading-behavior
 .   Seems like I mention it in the linked thread as well.

You then need to ensure no sqlalchemy-session-bound objects in your web 
application are shared between threads.  When you put things into your HTTP 
session, assuming its an in-memory HTTP session, make sure objects that go in 
there arent attached to a SQLAlchemy session.  Use expunge() or similar for 
this.  Calling remove() at the end of the request will work, except for the 
fact that a concurrent thread might be accessing the HTTP session before you 
get that far.I would recommend using cookie based HTTP sessions in any case 
(see Beaker for this functionality).

 Using Mike Bayer's _threading_local patch in thread [1] made no
 difference.

That thread regarded someone using an extremely rare tool called PyISAPIe, 
which had threading bugs in it.  That doesn't apply to a basic mod_wsgi 
configuration.

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