> On Nov 27, 2014, at 5:44 AM, Giovanni Cavallero > <giovanni.cavall...@ge.infn.it> wrote: > > typing on shell > > >>> s=DBSession() > >>> tube1=MaPMTs(Serial_Number='FA0026',Gain=1.18) > >>> s.add(tube1) > >>> s.commit() > >>> tube2=MaPMTs(Serial_Number='FA0026',Gain=1.08) > >>> s.add(tube2) > >>> s.commit() > >>> tube1_update = > >>> s.query(MaPMTs).filter(MaPMTs.Serial_Number=='FA0026').first() > >>> tube1_update.Validity_End = tube2.Validity_Start > >>> s.commit() > > > > I would obtain the same table without invoke a query. Is it possibile to add > some new lines in the code to do the same thing?
SQLAlchemy is a system that ultimately is only emitting SQL. If you need to retrieve data from one row and INSERT it into a new row, you have to decide from a SQL point of view how you’d like that to look. As I mentioned earlier, to use just one statement instead of two means you’re combining a SELECT and an INSERT together. There are a variety of ways to do this, so you’d need to decide which one is most appropriate for you. You can assign a select() object to an attribute using the approach described in http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#embedding-sql-insert-update-expressions-into-a-flush <http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#embedding-sql-insert-update-expressions-into-a-flush>, which here would look like: tube1 = Tube(…) tube1.validity_end = select([Tube.validity_start]).where(Tube.serial_number = ‘xyz’) or you could use INSERT from SELECT as described at http://docs.sqlalchemy.org/en/rel_0_9/core/dml.html?highlight=insert%20from%20select#sqlalchemy.sql.expression.Insert.from_select <http://docs.sqlalchemy.org/en/rel_0_9/core/dml.html?highlight=insert%20from%20select#sqlalchemy.sql.expression.Insert.from_select>. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.