mercredi 9 mars 2016

update joined table using sqlalchemy core

I want to update the values in a table dependent on values in another table.

I have 2 tables:

_LOCATIONR: _id, _status, _uuid

LOCATIONR: _id, _version and some userdata

_status shall be set to 1 if _version=0, _uuid="42" and _id=1

i use python with sqlalchemy, database is sqlite

sc#table LOCATIONR
sc2#table _LOCATIONR
version = 0
uuid = "42"
id = 1
statement = sc2.update().
                where(and_(
                    sc2.c._id == id,
                    sc2.c._id == sc.c._id,
                    sc.c._version == version,
                    sc2.c._uuid == uuid))
r = statement.execute({"_status": 1, "_id": id, "_uuid": uuid})

Log:

2016-03-09 22:08:45,059 INFO sqlalchemy.engine.base.Engine UPDATE "_LOCATIONR" SET _uuid=?, _status=?, _id=? FROM "LOCATIONR" WHERE "_LOCATIONR"._id = ? AND "_LOCATIONR"._id = "LOCATIONR"._id AND "LOCATIONR"._version = ? AND "_LOCATIONR"._uuid = ?
2016-03-09 22:08:45,059 INFO sqlalchemy.engine.base.Engine ('42', 1, 1, 1, 0, '42')
2016-03-09 22:08:45,060 INFO sqlalchemy.engine.base.Engine ROLLBACK

Everytime I execute the above code sqlalchemy makes a rollback. The errors occur only if the whereclause references sc but due to those links it should be ok http://ift.tt/223PjLY

Update a Joined Table with SQLAlchemy Core

Aucun commentaire:

Enregistrer un commentaire