Daniel Miller wrote:
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
For a database that supports triggers, you can define a trigger before insert that checks for an existing row and deletes it. The trigger function could also just update the existing row with the new values instead of deleting the row.. and then discard the insert.

Of course, it's a whole different topic for discussion on whether or not you should program the application that way. :)

-Dennis



-------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to