sqlalchemy.exc.UnboundExecutionError

1,341 views
Skip to first unread message

Jason Hoppes

unread,
May 17, 2022, 4:51:49 PM5/17/22
to sqlalchemy
I am getting an exception:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper mapped class User->users, SQL expression or this Session.

My engine creation code is in a static method of a class and is as follows:

            db_url = 'postgresql+psycopg2://myuser:'\
                     'mypw@localhost:5432/'\
                     'mydb'
            cls.engine = create_engine(db_url, echo=False)

at the module level I have this function:

def add_user(username, password,
             active=True, by=1,
             uid=None):
    """Create a User and add it to the database."""
    with Session(DatabaseORM.get_engine()) as session:
        if uid is not None:
            user = User(username=username,
                        password=password,
                        active=active,
                        created_by=by,
                        uid=uid)
        else:
            user = User(username=username,
                        password=password,
                        active=active,
                        created_by=by)

        session.add(user)

        session.commit()
        id_ = user.id_

    return id_

I tried adding:

session = Session.configure(bind=self.engine)

in the with block but it told me configure is not an attribute of session.

Any help would be appreciated thank you.

- Jason

Jason Hoppes

unread,
May 17, 2022, 5:09:50 PM5/17/22
to sqlalchemy
I found my error It was in the logic. Thank you for taking the time to read.

- Jason
Reply all
Reply to author
Forward
0 new messages