简体   繁体   中英

object is added to the database without session.commit()

I run into a weird issue using SQLAlchemy in my Flask API application, an object is inserted into the database even though the commit instruction is commented:

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

any explications ?

EDIT:

here is the class Projects:

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

You session has probably been created with autocommit=True flag. See where the object is created and change that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM