Jonathan Hayward http://JonathansCorner.com wrote:
Is there a way to specify an insert that says "Overwrite any previous rows containing this row's primary key"?


In SQL that's two queries:

 delete from users where user_id = ?
 insert into users (user_id, ...) values (?, ...)

SQLAlchemy can do that quite succinctly:

def overwrite(id, ...):
   users.delete(users.c.user_id==id)
   users.insert().execute(user_id=id, ...)

Note: by definition, if it's a primary key then there will be at most one row 
with a given key value. Therefore, there will be at most one row affected by 
the delete statement.

~ Daniel



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to