[Python] sqlalchemy Session object has no attribute add

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to insert the data to mysql server, While doing so when i try to add the data into the SQLalchemy sesion i am getting the error "Generator object has no attribute add"

    db=get_db()
    temp = schema.User(**filtered_dict)
    insert_data=models.User(**temp.dict())
    db = get_db()
    db.add(insert_data)
    db.commit()
    db.refresh()


    Session generator:

    def get_db():

    db_session = sessionlocal()
    try:
    yield db_session
    finally:
    db_session.close()


    session creation


    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker

    SQLALCHEMY_DATABASE_URL = "mysql+mysqlconnector://root:password@localhost:3305/line_api"

    engine = create_engine(
    SQLALCHEMY_DATABASE_URL
    )
    sessionlocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


    Base = declarative_base()


    please help...

    Continue reading...

Compartilhe esta Página