在查询中使用过滤器,然后使用func.concat更新无效

时间:2016-07-13 10:13:26

标签: python sqlalchemy

我遇到了一个问题,为什么不是所有的更新?

示例如下:

modal.py

# coding=utf-8
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()

class Project(Base):
    __tablename__ = 'project'

    id = Column(Integer, primary_key=True)
    name = Column(String(45), default='')
    change_log = Column(Text)

使用测试数据,表结构如下:

+----+----------+-------------+
| id | name     | change_log   |
+----+----------+--------------
|  1 | Platform | 1234         |
|  2 | vip      | 5678         |
+----+----------+-------------

的示例:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func
from modal import Project

engine = create_engine('mysql+pymysql://root:123456789@localhost:3306/db?charset=utf8')
Session = sessionmaker(bind=engine, autoflush=False)
session = Session()

project_list = ['platform', 'vip']
content = 'this is test text'

obj = session.query(Project).filter(Project.name.in_(project_list))
obj.update({'change_log': func.concat(Project.change_log, content)}, synchronize_session='fetch')
session.commit()
session.close()

但只有平台updates.result:

+----+----------+----------------------+
| id | name     | change_log           |
+----+----------+----------------------
|  1 | Platform | 1234this is test text|
|  2 | vip      | 5678                 |
+----+----------+-----------------------

感谢。

0 个答案:

没有答案