sqlalchemy中同一个表中另一列的sql update列

时间:2015-02-16 10:47:28

标签: python sqlalchemy

UPDATE table1 SET column1 = column2 WHERE column3 ='abc';

如何在sql alchemy ORM中执行上述查询

table1 = Table('table1', metadata,
    Column("column1",     SmallInteger(),     nullable=False, autoincrement=False),
    Column("column2",     SmallInteger(),    nullable=False, autoincrement=False),
    Column("column3",     String(length=255), nullable=False)
)

class Table1(object):
    pass
mapper(Table1, table1)

1 个答案:

答案 0 :(得分:1)

DBSession.query(table1).filter_by(column3 = 'abc').update({"column1":column2}, synchronize_session=False)

使用此

where  ==== filter_by
table1 === class containing the table1 schema
update command with columns as key in query

http://docs.sqlalchemy.org/en/latest/core/dml.html点击此链接将指导您