Could not locate a bind configured on mapper using classical mapping
bottle, python, python-2.7, sqlalchemy
Solution
For some reason SQLAlchemy 0.8 doesn't like it when I do this (creating instance variables):
class BaseService(object):
def __init__(self):
self._engine = create_engine('mysql://root@localhost/my_db', pool_recycle = 3600)
self._Session = sessionmaker(bind = self._engine)
self._session = Session()
The fix was to make them static instead:
class BaseService(object):
engine = create_engine('mysql://root@localhost/my_db', pool_recycle = 3600)
Session = sessionmaker(bind = engine)
session = Session()
Then you can do:
class UserService(BaseService):
def create(self, data):
BaseService.session.query(UserDo).first()
Problem
I'm attempting to load a User object from mysql but I keep getting UnboundExecutionError: Could not locate a bind configured on mapper Mapper|UserDo|user, SQL expression or this Session. I'm using classical mappings. ``` Base = declarative_base() # A default constructor is created if one is not already present, # which accepts keyword arguments of the same name as that of the mapped attributes. class UserDo(Base): __tablename__ = 'user' id = Column(Integer, primary_key = True) fname = Column(String(100)) lname = Column(String(100)) email = Column(String(200)) salt = Column(String(100)) created_on = Column(TIMESTAMP) # from sqlalchemy.dialects.mysql import TIMESTAMP class BaseService(object): def __init__(self): self._engine = create_engine('mysql://root@localhost/my_db', pool_recycle = 3600) self._Session = sessionmaker(bind = self._engine) self._session = Session() class UserService(BaseService): def create(self, data): print self._session.query(UserDo).first() # Error ``` I'm wondering if I'm getting the error because of my create_engine statement. Maybe I'm not supplying the correct format for the connection. I don't have a password for my local database. Also, something else that I noticed: print self._session.query(UserDo) prints SELECT "user".id AS user_id, "user".fname AS user_fname, "user".lname AS user_lname, "user".email AS user_email, "user".salt AS user_salt, "user".created_on AS user_created_on FROM "user" Which is a syntactically incorrect. Either way I don't care what SQLAlchemy is doing internally, as long as doing User.fname, User.lname (etc), works as defined. Anyone see what's going on?