[sqlalchemy] about commit()

2010-04-23 Thread jo

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
   specie_codice='C',
   specie_descrizione='cat'
)

session.commit()

thanks for any help

j

--
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] about commit()

2010-04-23 Thread Mariano Mara
Excerpts from jo's message of Fri Apr 23 03:16:21 -0300 2010:
 Hi all,
 
 I need to insert a new row and get back the last inserted id,
 I have some difficulty using the flush(), then I'm trying with commit() but
 I can't understand how commit() works in 0.6.
 In the following script I try to update a row and it works properly
 but when I try to insert a new one, it doesn't work,
 there's no messages but the row is not inserted into the table.
 Is this the right way ?
 
 
 from sqlalchemy.orm import sessionmaker
 Session = sessionmaker(autoflush=True)
 session = Session()
 
 #update an existing row... it works
 old = Specie.get('D')
 old.specie_descrizione='dog'
 
 #insert a new row... it doesn't work
 new=Specie(
 specie_codice='C',
 specie_descrizione='cat'
 )
 
 session.commit()
 
 thanks for any help
 
 j
 
You forgot to add the instance to the session before the commit. See
http://www.sqlalchemy.org/docs/ormtutorial.html#adding-new-objects

-- 
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] further restricting a query provided as raw sql

2010-04-23 Thread Chris Withers

I can't reconcile your comment with mine...

...I want to limit the results returned to those matching a set of ids.
This isn't about storing schemaless content ;-)

Any serious ideas or should I just go sulk in the corner?

Chris

Michael Bayer wrote:

yeah man , this is why we're all moving to mongodb  :)


On Apr 15, 2010, at 10:46 AM, Chris Withers wrote:


Michael Bayer wrote:

you have to rewrite your SQL to support the number of values in the IN clause 
for each parameter set.

Hmm :'(

While my code knows the number of values, they don't, and it may vary from when 
they write the SQL to when that SQL gets executed by my code...

Chris

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





--
Simplistix - Content Management, Batch Processing  Python Consulting
- http://www.simplistix.co.uk

--
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] about commit()

2010-04-23 Thread jose soares

Mariano Mara wrote:

Excerpts from jo's message of Fri Apr 23 03:16:21 -0300 2010:
  

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)

session.commit()

thanks for any help

j



You forgot to add the instance to the session before the commit. See
http://www.sqlalchemy.org/docs/ormtutorial.html#adding-new-objects

  

Thank you Marionao for replay to my question.
I tried as you suggest, but now it raises an InvalidRequestError, take a 
look:


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#insert a new row... it doesn't work
new=Specie(
   specie_codice='C',
   specie_descrizione='cat'
)
session.add(new)
session.commit()

InvalidRequestError: Object 'Specie at 0x14ca650' is already attached 
to session '54658512' (this is '21800720')



--
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] sql particle as bind parameter option:

2010-04-23 Thread Paul Balomiri

Hi,

Several days ago i has a similar question, which
was, perhaps a bit too theoretical. Here is a simple case:

select(text(select * from f(:time)) )

can I supply alternatively either
{time: datetime.datetime(1999,9,9,9,9,9)}
or
{time:  now()}
 I cannot get around that sqlalchemy interprets   now() as a  
string, while i try to use the postgres(or other backends) now()  
function.




Paul Balomiri
paulbalom...@gmail.com



--
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] sql particle as bind parameter option:

2010-04-23 Thread Paul Balomiri

Hi,

Thanks, for your reply. I was aware that it is not just a string  
substitution,

and that the bindparams also maps the type of the parameters e.t.c.

But is it possible to supply a type /set of types which are or  
represent a lexical sub-structure ?
I might go into doing it, but first i want to check that i'm not  
reinventing the wheel.


I noticed that the where clause can be supplied several times, so
select( [my_table_metadata]). where(). where(...) is valid, thus  
it is possible
for this special case to insert a logical expression after the initial  
select() definition.


I am looking for a similar, but more general case, not necessarily  
using bindparam where  func.now()
would be a valid substitution or insertion element into a preexisting  
expression.


regards
Paul Balomiri
paulbalom...@gmail.com



On 23.04.2010, at 15:26, Michael Bayer wrote:



On Apr 23, 2010, at 6:58 AM, Paul Balomiri wrote:


Hi,

Several days ago i has a similar question, which
was, perhaps a bit too theoretical. Here is a simple case:

select(text(select * from f(:time)) )

can I supply alternatively either
{time: datetime.datetime(1999,9,9,9,9,9)}
or
{time:  now()}
I cannot get around that sqlalchemy interprets   now() as a  
string, while i try to use the postgres(or other backends) now()  
function.


bind parameters exist only for the purpose of specifying literal  
data to be injected as the values into a SQL statement.   They  
cannot be used to modify the lexical structure of a statement.   So  
a lexical unit like CURRENT TIMESTAMP or NOW()  is part of the  
statement's lexical source and cannot be injected as a bind parameter.


To put it another way, bind parameters are not just string  
substitutions the way something like foo %s % ('bar') is in  
Python, even though they may look that way.



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



Re: [sqlalchemy] about commit()

2010-04-23 Thread jose soares

jo wrote:

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with 
commit() but

I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
   specie_codice='C',
   specie_descrizione='cat'
)

session.commit()

thanks for any help

j

in my disperation, I tried also the following, but without success: :-( 


from sqlalchemy.orm.session import Session
session=Session(autoflush=True,autocommit=True)

class Gruppo:
   pass

mapper(Gruppo,
  tbl['gruppo'],
  column_prefix = 'gruppo_',
   )



session.begin()
 sqlalchemy.orm.session.SessionTransaction object at 0x28a9710

new=Gruppo( gruppo_id = 1, gruppo_descrizione='cat')
session.commit()
print Gruppo.get(1)

SELECT gruppo.id AS gruppo_id, gruppo.descrizione AS gruppo_descrizione
FROM gruppo
WHERE gruppo.id = %(param_1)s

Col ('gruppo_id', 'gruppo_descrizione')
None

I don't understand what's wrong. I can't INSERT a new record into a table.
Could someone, give me some help?

j




--
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] about commit()

2010-04-23 Thread Lance Edgar

On 4/23/2010 9:19 AM, jose soares wrote:

jo wrote:

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with
commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)

session.commit()

thanks for any help

j


in my disperation, I tried also the following, but without success: :-(
from sqlalchemy.orm.session import Session
session=Session(autoflush=True,autocommit=True)

class Gruppo:
pass

mapper(Gruppo,
tbl['gruppo'],
column_prefix = 'gruppo_',
)



session.begin()
  sqlalchemy.orm.session.SessionTransaction object at 0x28a9710

new=Gruppo( gruppo_id = 1, gruppo_descrizione='cat')
session.commit()
print Gruppo.get(1)

 SELECT gruppo.id AS gruppo_id, gruppo.descrizione AS gruppo_descrizione
 FROM gruppo
 WHERE gruppo.id = %(param_1)s

 Col ('gruppo_id', 'gruppo_descrizione')
 None

I don't understand what's wrong. I can't INSERT a new record into a table.
Could someone, give me some help?


Would this (not) work?

from sqlalchemy import *
from sqlalchemy.orm import mapper

metadata = MetaData()
groups = Table('groups', metadata, Column('id', Integer, primary_key=True), 
Column('name', String(25)))

class Group(object):
pass

mapper(Group, groups)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker()

session = Session()
group = Group()
group.name = 'cat'
session.add(group)
session.commit()

session.expunge_all()
group = session.query(Group).first()
print group.id
session.close()

--
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] about commit()

2010-04-23 Thread jose soares

Lance Edgar wrote:

On 4/23/2010 9:19 AM, jose soares wrote:

jo wrote:

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with
commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)

session.commit()

thanks for any help

j


in my disperation, I tried also the following, but without success: :-(
from sqlalchemy.orm.session import Session
session=Session(autoflush=True,autocommit=True)

class Gruppo:
pass

mapper(Gruppo,
tbl['gruppo'],
column_prefix = 'gruppo_',
)



session.begin()
  sqlalchemy.orm.session.SessionTransaction object at 0x28a9710

new=Gruppo( gruppo_id = 1, gruppo_descrizione='cat')
session.commit()
print Gruppo.get(1)

 SELECT gruppo.id AS gruppo_id, gruppo.descrizione AS 
gruppo_descrizione

 FROM gruppo
 WHERE gruppo.id = %(param_1)s

 Col ('gruppo_id', 'gruppo_descrizione')
 None

I don't understand what's wrong. I can't INSERT a new record into a 
table.

Could someone, give me some help?



session.commit() raises an UnBoundExecutionError:


Would this (not) work?

from sqlalchemy import *
from sqlalchemy.orm import mapper

metadata = MetaData()
groups = Table('groups', metadata, Column('id', Integer, 
primary_key=True), Column('name', String(25)))


class Group(object):
pass

mapper(Group, groups)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker()

session = Session()
group = Group()
group.name = 'cat'
session.add(group)
session.commit()
UnboundExecutionError: Could not locate a bind configured on mapper 
Mapper|Group|groups or this Session


session.expunge_all()
group = session.query(Group).first()
print group.id
session.close()



--
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] about commit()

2010-04-23 Thread Lance Edgar

Sorry, I'd meant for that code to be self-contained but of course I forgot to 
set up the engine.  As an example you can try the following (see inserted code 
below).

Lance

On 4/23/2010 9:50 AM, jose soares wrote:

session.commit() raises an UnBoundExecutionError:


Would this (not) work?

from sqlalchemy import *
from sqlalchemy.orm import mapper


engine = create_engine('sqlite:///:memory:')


metadata = MetaData()
groups = Table('groups', metadata, Column('id', Integer,
primary_key=True), Column('name', String(25)))


metadata.create(bind=engine)


class Group(object):
pass

mapper(Group, groups)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker()

session = Session()


session.bind = engine


group = Group()
group.name = 'cat'
session.add(group)
session.commit()

UnboundExecutionError: Could not locate a bind configured on mapper
Mapper|Group|groups or this Session


session.expunge_all()
group = session.query(Group).first()
print group.id
session.close()


--
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] list filter

2010-04-23 Thread Alexander Zhabotinskiy
Hello.

How to filter by list
may be like:

.filter(Some.value==[1,2,3])

-- 
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] list filter

2010-04-23 Thread Lance Edgar

On 4/23/2010 9:33 AM, Alexander Zhabotinskiy wrote:

Hello.

How to filter by list
may be like:

.filter(Some.value==[1,2,3])



I believe you want the IN filter; see 
http://www.sqlalchemy.org/docs/ormtutorial.html#common-filter-operators.

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] list filter

2010-04-23 Thread Michael Mileusnich
Hello,

I believe this is what you are looking for:

.filter(obj.in([1, 2, 3]))

On Fri, Apr 23, 2010 at 9:33 AM, Alexander Zhabotinskiy 
a.zhabotins...@gmail.com wrote:

 Hello.

 How to filter by list
 may be like:

 .filter(Some.value==[1,2,3])

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



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



Re: [sqlalchemy] sql particle as bind parameter option:

2010-04-23 Thread Michael Bayer


On Apr 23, 2010, at 9:59 AM, Paul Balomiri paulbalom...@gmail.com  
wrote:



Hi,

Thanks, for your reply. I was aware that it is not just a string  
substitution,

and that the bindparams also maps the type of the parameters e.t.c.

But is it possible to supply a type /set of types which are or  
represent a lexical sub-structure ?
I might go into doing it, but first i want to check that i'm not  
reinventing the wheel.


I noticed that the where clause can be supplied several times, so
select( [my_table_metadata]). where(). where(...) is valid, thus  
it is possible
for this special case to insert a logical expression after the  
initial select() definition.


I am looking for a similar, but more general case, not necessarily  
using bindparam where  func.now()
would be a valid substitution or insertion element into a  
preexisting expression.




Why not just create a python function that generatively produces the  
desired statement based on arguments?  I don't see the advantage to  
something more magical than that.





regards
Paul Balomiri
paulbalom...@gmail.com



On 23.04.2010, at 15:26, Michael Bayer wrote:



On Apr 23, 2010, at 6:58 AM, Paul Balomiri wrote:


Hi,

Several days ago i has a similar question, which
was, perhaps a bit too theoretical. Here is a simple case:

select(text(select * from f(:time)) )

can I supply alternatively either
{time: datetime.datetime(1999,9,9,9,9,9)}
or
{time:  now()}
I cannot get around that sqlalchemy interprets   now() as a  
string, while i try to use the postgres(or other backends) now()  
function.


bind parameters exist only for the purpose of specifying literal  
data to be injected as the values into a SQL statement.   They  
cannot be used to modify the lexical structure of a statement.   So  
a lexical unit like CURRENT TIMESTAMP or NOW()  is part of the  
statement's lexical source and cannot be injected as a bind  
parameter.


To put it another way, bind parameters are not just string  
substitutions the way something like foo %s % ('bar') is in  
Python, even though they may look that way.



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




--
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: list filter

2010-04-23 Thread Alexander Zhabotinskiy
in. YES

Thkz

-- 
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] Mapper table properties

2010-04-23 Thread Jason Baker
Given an entity class (or entity instance), I'd like to get the table that
is mapped to it.  If I get the mapper using object_mapper/class_mapper, then
I get a mapper with the following properties defined (among others):
local_table, mapped_table, and tables.  Can someone help me understand what
the difference between each of these properties is (or point me towards
documentation on them)?

-- 
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] about commit()

2010-04-23 Thread jose soares

Yes Lance, now it works, thank you v.m. :-)
j
Lance Edgar wrote:

On 4/23/2010 9:19 AM, jose soares wrote:

jo wrote:

Hi all,

I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with
commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?


from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()

#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'

#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)

session.commit()

thanks for any help

j


in my disperation, I tried also the following, but without success: :-(
from sqlalchemy.orm.session import Session
session=Session(autoflush=True,autocommit=True)

class Gruppo:
pass

mapper(Gruppo,
tbl['gruppo'],
column_prefix = 'gruppo_',
)



session.begin()
  sqlalchemy.orm.session.SessionTransaction object at 0x28a9710

new=Gruppo( gruppo_id = 1, gruppo_descrizione='cat')
session.commit()
print Gruppo.get(1)

 SELECT gruppo.id AS gruppo_id, gruppo.descrizione AS 
gruppo_descrizione

 FROM gruppo
 WHERE gruppo.id = %(param_1)s

 Col ('gruppo_id', 'gruppo_descrizione')
 None

I don't understand what's wrong. I can't INSERT a new record into a 
table.

Could someone, give me some help?


Would this (not) work?

from sqlalchemy import *
from sqlalchemy.orm import mapper

metadata = MetaData()
groups = Table('groups', metadata, Column('id', Integer, 
primary_key=True), Column('name', String(25)))


class Group(object):
pass

mapper(Group, groups)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker()

session = Session()
group = Group()
group.name = 'cat'
session.add(group)
session.commit()

session.expunge_all()
group = session.query(Group).first()
print group.id
session.close()



--
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] sql particle as bind parameter option:

2010-04-23 Thread Paul Balomiri




On 23.04.2010, at 17:03, Michael Bayer wrote:



On Apr 23, 2010, at 9:59 AM, Paul Balomiri paulbalom...@gmail.com  
wrote:



Hi,

Thanks, for your reply. I was aware that it is not just a string  
substitution,

and that the bindparams also maps the type of the parameters e.t.c.

But is it possible to supply a type /set of types which are or  
represent a lexical sub-structure ?
I might go into doing it, but first i want to check that i'm not  
reinventing the wheel.


I noticed that the where clause can be supplied several times, so
select( [my_table_metadata]). where(). where(...) is valid,  
thus it is possible
for this special case to insert a logical expression after the  
initial select() definition.


I am looking for a similar, but more general case, not necessarily  
using bindparam where  func.now()
would be a valid substitution or insertion element into a  
preexisting expression.




Why not just create a python function that generatively produces the  
desired statement based on arguments?  I don't see the advantage to  
something more magical than that.

Well, this is what i already do:

lazy_sql_partial = functools.partial(lambda x: select([...] ,  
from_obj(func.a(bindparam('a'),bindparam('b'),x   ))) )

and then i name the binding like so:
lazy_sql_unbound = (lazy_sql_partial, (funcarg))


At the time when i generate the sql expression I do not know the  
parameters, not even a default. Those are generated based on user input.
in case parameters are missing for a whole expression the whole  
expression is skipped.


A even simpler expression would be a=1 or a=2 or ... At the moment  
i cannot generate such a constuct, which allows me to
define a select in one place, and then later add a or_(1,2,...) clause  
at a certain point.As a special case i could use   select().where(1).  
where(2)... to get an and_(1,2,...)



An example :
Whenever the map window changes, i need to calculate the dataset which  
falls out of the current window, and the ones which drops in.
Additionally i want to query the a modified sql expression whenever  
the table changes (modified = with an additional in_ costraining to  
pks in the table)

to do both i need to regenerate parts of the sql, but not all of it.

The way i do it now seems rather ugly because:
1) (partial_bound_func, (funcarg)) is error prone ( I know i cold do  
more reflection to find out argument numbers and names , but it would  
in the end duplicate the bindparam mechanism )

2) to execute i have to:
	- first look for parameters which are arguments to the partial  
generative functions  myselect= lazy_sql_unbound( user input params  
for partial )

- then use the remaining parameters in session.execute( )

It just feels like all of this rather belongs into the lib, because it  
could be all solved by allowing bindparam to have a value of type  
SQLExpression.

moreover, the expression substitutions could accept bindparams as well.

As a last point , the compiler could check the validity, as it does  
already.










regards
Paul Balomiri
paulbalom...@gmail.com



On 23.04.2010, at 15:26, Michael Bayer wrote:



On Apr 23, 2010, at 6:58 AM, Paul Balomiri wrote:


Hi,

Several days ago i has a similar question, which
was, perhaps a bit too theoretical. Here is a simple case:

select(text(select * from f(:time)) )

can I supply alternatively either
{time: datetime.datetime(1999,9,9,9,9,9)}
or
{time:  now()}
I cannot get around that sqlalchemy interprets   now() as a  
string, while i try to use the postgres(or other backends) now()  
function.


bind parameters exist only for the purpose of specifying literal  
data to be injected as the values into a SQL statement.   They  
cannot be used to modify the lexical structure of a statement.
So a lexical unit like CURRENT TIMESTAMP or NOW()  is part of  
the statement's lexical source and cannot be injected as a bind  
parameter.


To put it another way, bind parameters are not just string  
substitutions the way something like foo %s % ('bar') is in  
Python, even though they may look that way.



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




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

Re: [sqlalchemy] list filter

2010-04-23 Thread Paul Balomiri

Hi,

That would be table_metadata.c.column_name.in_([1,2,3]),
If you are looking for an in relation. You could also use  
MappedObjectClass.property instead of the column object.


Otherwhise, if you are really testing for array equality your  
expression can be used. Also, in this latter case, make sure that  
there is a DB array type, with the db you are using.


look here for some examples and for API Docs
http://www.sqlalchemy.org/docs/_06/reference/sqlalchemy/expressions.html#functions

Paul Balomiri
paulbalom...@gmail.com



On 23.04.2010, at 16:33, Alexander Zhabotinskiy wrote:


Hello.

How to filter by list
may be like:

.filter(Some.value==[1,2,3])

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