我在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
答案 0 :(得分:0)
您session
可能是用autocommit=True
标志创建的。查看对象的创建位置并进行更改。