[sqlalchemy] Re: Default value for objects' attributes

2007-07-31 Thread Jonathan Ballet

On 30 juil, 22:33, Michael Bayer [EMAIL PROTECTED] wrote:
 yes, figured that.  The issues with this are that it would not be
 consistently available; it could only work for Python-side defaults,
 and could also only work for defaults that do not require an
 ExecutionContext

Ah, consistency, this is the word I was looking for :)

 Also if you really need the False before any flush occurs, you
 might want to define that in the __init__ method of your mapped class
 as well.  If you create an external function called my_table_default
 (), the same function can be placed within your Table def as well as
 your class's __init__ method.

Ok, I think we will use something like that :

class Test(object):
def __init__(self):
self.test_value = self.c.test_value.default.arg

... And so on (maybe a wrapper around self.c.test_value.default.arg,
to make it shorter).


Thanks a lot for your answers !


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-31 Thread Michael Bayer


On Jul 30, 2007, at 7:29 PM, Paul Johnston wrote:


 Hi,

 The problem is that creating a new object from class Test doesn't set
 the default value for attributes :


 Seems like a good time to bring up a slightly related problem. If you
 modify an object in a function providing a default value, the  
 object is
 not flushed (unless you do that manually). Ideally flushing would  
 detect
 this and cope with it. If defaults were supplied at constructor  
 time, we
 would get that behaviour with no further work.

not sure i understand the scenario here, could you illustrate ?


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-31 Thread Paul Johnston

Hi,

Seems like a good time to bring up a slightly related problem. If you
modify an object in a function providing a default value, the  
object is not flushed (unless you do that manually). 

not sure i understand the scenario here, could you illustrate ?
  

I've put a bit more explanation and a test case on ticket #703. I had a 
look at fixing it, but it seems like a tough one.

Paul

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-31 Thread Michael Bayer


On Jul 31, 2007, at 11:30 AM, Paul Johnston wrote:


 Hi,

 Seems like a good time to bring up a slightly related problem. If  
 you
 modify an object in a function providing a default value, the
 object is not flushed (unless you do that manually).

 not sure i understand the scenario here, could you illustrate ?


 I've put a bit more explanation and a test case on ticket #703. I  
 had a
 look at fixing it, but it seems like a tough one.


ok that exactly cannot be fixed, and i would suggest that you dont  
use an ORM mapped class as a default value generator.  The reason it  
cannot is because once the flush() is proceeding, everything which is  
to be inserted/updated/deleted has been laid out and placed into a  
queue to be executed.  Any changes to instrumented attributes which  
occur *within* the flush is not going to get picked up - that train  
has already left the station.

the way to do this pattern is to use straight SQL instead (note  
however, the 'ctx' variable requires 0.4):

seq = Table('seq', m,
 Column('seq', String(50), primary_key=True),
 Column('nextval', Integer))

def newid(ctx):
 id = ctx.connection.execute(select([seq.c.nextval],  
seq.c.seq=='tbl', for_update=True)).scalar()
 ctx.connection.execute(seq.update(values=[seq.c.nextval :  
seq.c.nextval +1]))
 return hex(id)

This way you also get fine-grained control over the seq table's locking.

However I do need to commit something to make that work so that will  
be the ticket's resolution.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-30 Thread Michael Bayer

Cant reproduce.  Heres a test script which runs fine for me with PG 8.1:

from sqlalchemy import *

metadata = MetaData('postgres://scott:[EMAIL PROTECTED]/test')

test = Table('test', metadata,
 Column('id', Integer, primary_key=True),
 Column('test_value', Boolean, default=False, nullable=False)
)
metadata.create_all()

class Test(object):pass
test_mapper = mapper(Test, test)

t = Test()
sess = create_session()
sess.save(t)
sess.flush()

print t.test_value


did you forget to flush() ?




--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-30 Thread sdobrev

your _own_ ctor, or something around mapperExtension?

On Monday 30 July 2007 21:26:44 Jonathan Ballet wrote:
 No, after a flush(), everything is fine.
 However, I would like to have the default value _before_
 flush()-ing.

 Hmm, after thinking about it a few more minutes, it would be a bit
 restrictive, since it will work only for 'scalar' values :/

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-30 Thread Michael Bayer


On Jul 30, 2007, at 2:26 PM, Jonathan Ballet wrote:

 No, after a flush(), everything is fine.
 However, I would like to have the default value _before_ flush()-ing.

 Hmm, after thinking about it a few more minutes, it would be a bit
 restrictive, since it will work only for 'scalar' values :/

yes, figured that.  The issues with this are that it would not be  
consistently available; it could only work for Python-side defaults,  
and could also only work for defaults that do not require an  
ExecutionContext (i.e. the full set of parameters being passed to the  
INSERT statement).   The usage model with SA, and especially in 0.4,  
is that data can be flushed anytime...no need to wait for some  
special time to flush.  with that model, your defaults are availble  
in all cases the way youd want them.

Also if you really need the False before any flush occurs, you  
might want to define that in the __init__ method of your mapped class  
as well.  If you create an external function called my_table_default 
(), the same function can be placed within your Table def as well as  
your class's __init__ method.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Default value for objects' attributes

2007-07-30 Thread Paul Johnston

Hi,

The problem is that creating a new object from class Test doesn't set
the default value for attributes :
  

Seems like a good time to bring up a slightly related problem. If you 
modify an object in a function providing a default value, the object is 
not flushed (unless you do that manually). Ideally flushing would detect 
this and cope with it. If defaults were supplied at constructor time, we 
would get that behaviour with no further work.

Paul

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---