Assuming you're talking about the ORM.  You can of course emit the exact SQL 
you refer to any time using execute() from an engine, connection, or session.   
The ORM works within a transaction at all times.    so an expression like:

myobject = MyObject(id=5)
myobject = Session.merge(myobject)
Session.commit()

will perform the equivalent steps, subject to the transaction isolation in 
effect.

To lock the row during the SELECT, you can use 
query.with_lockmode(True).filter(criterion) to SELECT the row with FOR UPDATE, 
will return None if the row does not exist.






On Jun 13, 2011, at 1:23 PM, Moch Ramis wrote:

> Hello.
> 
> I am trying to create a transaction that should insert a value
> depending of the result of a select. This should be done at once by
> the database in order to avoid some conflicts or duplicates.
> An equivalent sql version of the transaction would be :
> 
> IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
>    INSERT INTO Table1 VALUES (...)
> 
> Is there a way to do that using sqlalchemy ?
> Having the "if" done by python is easy but not safe and i can't find
> out how to do this using some begin/commit statements.
> 
> Thanks to help !
> 
> -- 
> 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 
> 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 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.

Reply via email to