Python Pony ORM记录更新异常

时间:2017-02-22 22:00:42

标签: python-2.7 orm ponyorm

在Python ORM Pony中,如果我尝试插入记录但它已经存在,我想尝试更新记录。

记录已存在时收到以下错误: a:link { text-decoration:none; color:#008b45; } a:hover { margin-bottom: 3px solid #ff7400; background:white; } a:visited { color:#ee9a00; }

编辑:但是,即使我尝试更新记录,似乎也会重新抛出此错误。这是我高度简化的示例代码:

pony.orm.core.TransactionIntegrityError: Object Record[1234] cannot be stored in the database. IntegrityError: UNIQUE constraint failed: Record.id

思想?

1 个答案:

答案 0 :(得分:0)

这对我有用

sql_debug(True)
records = [{"id":1233},{"id":1236}]

def saveRecords(records):
    with db_session:
        for r in records:
            try:
                print "inserting"
                print r
                Record(**r)
                commit()
            except Exception as e:
                print("error caught")
                print e 
            #p = Record.get(id=r.get("id"))
            #p.set(**r)

saveRecords(records)