Re: [sqlalchemy] How-to : update A also create an update of B

2012-08-31 Thread Tony Moutaux
Le jeudi 30 août 2012 17:16:40 UTC+2, Michael Bayer a écrit :

 def after_flush(session, flush_context):
 my_bs = figure_out_bs(session)
 new_session = Session(bind=session.connection())
 new_session.add_all(my_bs)

 # this won't actually COMMIT the transaction as we are 
 # already inside one
 new_session.commit()

 As I'm not the only developer for this application, I can't use pure SQL 
commands. 

I've tried the after_flush solution. It works.


What could be the problem to use setter ? For example

Class A has attribut family_id

@family_id.setter
def family_id(self, family_id):
   self._family_id = family_id
   update_linked_entities_with_family(family_id)

with update_linked_entities_with_family a function that grab and modify Bs.


You point that it could be used only for changing Python attributs. I use 
it to change SQLAlchemy entities and fields.

What could be the drawback of this solution ?

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



[sqlalchemy] How-to : update A also create an update of B

2012-08-30 Thread Tony Moutaux
Hi there !

I'm going mad looking for a solution for what seems a simple problem.

When object A is updated, I also want object B to be updated, using some 
computation based on A new values.
From now, I can detect when A is modified using after_update.

What I try is to look at event Session.after_flush, if in Session.dirty 
there is an instance of A, I call the update of B. Within after_flush I can 
see Session.dirty with the modified B. But no SQL UPDATE is issued at all.

Any tip for me ?

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



[sqlalchemy] Re: How-to : update A also create an update of B

2012-08-30 Thread Tony Moutaux
A friend point me out the hybrid_property and the fact I can detect an 
update of A fields before doing the commit, issuing some update for B. I'll 
try this idea.

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



Re: [sqlalchemy] How-to : update A also create an update of B

2012-08-30 Thread Tony Moutaux


Only if some computation based on A new values means that there are 
 SQL-level functions which you need to get at, does the rationale for 
 after_update/after_flush events to come up.Unfortunately you can't 
 emit a flush() within any of these events, as they are already local to the 
 flush() call itself.Nor can you change the state of the Session within 
 any of the after_ events, as the Session is already doing its work.  You 
 can add new state to the Session within the after_flush_postexec() 
 method, however you'd need to call flush() again for those changes to be 
 flushed.


The computation will modify B, which is also a map of a table. 

So the options:

 1. if some computation is only in-Python computation, and *not* SQL 
 functions, then set the state of B within object-level events, such as the 
 __init__() of A, a @property on A, @validates events on A, or attribute 
 events on A.


I've used something like this :

A has field family_id, which trigger the modification of B (and not anly B, 
but also some more tables)

@family_id.setter
def family_id(self, family_id):
self._family_id = family_id
self.update_linked_entities_with_family(family_id)

Then at session.commit(), A is updated, but also the other linked entities.

This works. But it's not really nice, because any changes, even rollbacked 
one will call the update_linked_entitites method.


 2.if some computation involves SQL functions:

 a. emit your modifications to B using only SQL statements and 
 Session.execute(), not by adding any new state to the Session or 
 re-flushing.

 b. pre-execute your SQL functions for A *outside* of the flush, then 
 apply values to B as needed.

 c. use a second Session inside the event:

 def after_flush(session, flush_context):
 my_bs = figure_out_bs(session)
 new_session = Session(bind=session.connection())
 new_session.add_all(my_bs)

 # this won't actually COMMIT the transaction as we are 
 # already inside one
 new_session.commit()


I can read this, but I don't understand how to do this and not exactly how 
it works. It is 3 solutions right , not 3 points I have to go through ?

Step A : calling only Session.execute will output direct SQL commands, thus 
not modify the Session dirty list and the state.
Seems quiet easy but less readable because of the direct SQL commands.

Step B : what *outside* of the flush means ?

Step C : I will try this one because it seems nice, using 
Session/transaction.


Thank you for the explanation. I have a lot to do to fully understand 
SQLAlchemy.

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



[sqlalchemy] Re: Table name without quoting with upper case

2010-11-26 Thread Tony Moutaux
On 25 nov, 16:28, Michael Bayer mike...@zzzcomputing.com wrote:
 I did a little googling and Sybase can handle the quotes, if you set set 
 quoted_identifier on on your database.  
 (http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@ebt-li...)

Yep ! That's kind of solution, also setting this in the sybase engine
cause some other kind of problems with some tables with very weird
definition (but until I use them, it's not an issue)


 Which leaves us at the only answer you'll likely care about, just set 
 quote=False on your Table object.   Good luck !

Nice !

I have found the quote attribut of the Table class in the SA doc, try
it and it's all good. Now I just have to put it in the right place in
my pylons files (which is not hard to do : at the end of the model
file where the class is defined).

Thanks a lot for this quick answer.
Tony

-- 
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] Table name without quoting with upper case

2010-11-25 Thread Tony Moutaux
Hi there !

I'm a newby du SA and try to make it work with our production
platform, using Sybase and old and scarry database schema, with table
names like Prestation , with 1 upper case.

Sybase does not allow (to my knowledge) tu use something like
select Prestation.id

So I try to figure how to avoid the quoting of the tablename. I know,
I read that if tablename is not lower case, it's quoted. But I can't
afford it.

I'm drilling into SA to find where the quoting is done (looking in
schema.py at the moment). But if somebody can point me to a prettier
solution than adding my piece of crap code for this case, it would be
great !

Tony

PS : platform
Sybase ASE 15.5
SQLAlchemy 0.6.5 with pyodbc over FreeTDS/Fedora

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