对象被添加到数据库而没有session.commit()

问题描述 投票:0回答:1

我在SQLAlchemy应用程序中使用Flask API遇到了一个奇怪的问题,即使提交指令被注释,也会将一个对象插入到数据库中:

try:
  new_project = model.Projects(project_name, project_desc)
  session.add(new_project)
  session.flush()
  session.refresh(new_project)
  # session.commit()
  response = 'OK'
  return response
except Exception as e:
    logging.error("ERROR :"+str(e))  
    session.rollback()
    response = 'ERROR'
    session.close()
    return response

任何解释?

编辑:

这是课程项目:

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class Projects(Base, DictSerializable):
    __tablename__ = 'projects'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    description = Column(String)


    def __init__(self, name, description=None):
        self.name = name
        self.description = description
python sqlalchemy
1个回答
0
投票

session可能是用autocommit=True旗帜创造的。查看对象的创建位置并进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.