[PonyORM-list] Create or Update

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Wed Aug 27 22:25:37 UTC 2014


Currently Pony doesn't have have support for "upserts". The main problem
lies in PostgreSQL - it is technically impossible to implement "create or
update" correctly in PostgreSQL, and any possible solution has at least one
problem - possible deadlock, race condition, lost update, etc.
http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/
Because of this is is not clear to me how database-independent API for
"create or update" command should look in Pony.

The StackOverflow answer that you mention give an SQL example of "INSERT
... ON DUPLICATE UPDATE" for MySQL. Currently you can execute such command
in Pony using raw SQL. For example you can use Database.execute() method,
something like this:

    pk_value = 123  # this is the value of primary key
    increment = 1    # I want to update other column with this value
    db.execute("""
        INSERT INTO MyTable (pk_column, other_column) VALUES($pk_value,
$increment)
        ON DUPLICATE KEY UPDATE other_column = other_column +
VALUES(other_column)
    """)





On Wed, Aug 27, 2014 at 5:28 AM, Jake Austwick <jake.austwick at gmail.com>
wrote:

> Does Pony have functionality to create or update if existing a model in
> one query, or do I have to check with exists() first?
>
> Example: http://stackoverflow.com/a/1218909/2175384
>
> I couldn't find a way to do this in one query in the documentation, and if
> it currently isn't in there, is it a planned feature?
>
> Thanks,
> --
> Jake Austwick
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140828/3bf17f39/attachment.html>


More information about the ponyorm-list mailing list