Sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session

ⅰ亾dé卋堺 提交于 2019-12-22 04:34:14

问题


I create a class for working with SQLAlchemy :

class DbAbsLayer(object):
    def __init__(self):
        self.setConnectionURI();
    def setConnectionURI(self):
        self.dbDriver = "mysql";
        self.dbHostname = "localhost";
        self.dbUsername = "root";
        self.dbPassword = "123";
        self.dbName = "mydbname";
    def createSession(self):
        Session = sessionmaker();
        self.session = Session.configure();
        self.session = Session();
    def createEngine(self):
        self.setConnectionURI();
        self.engine = create_engine(self.dbDriver + "://" + self.dbUsername + ":" + self.dbPassword + "@" + self.dbHostname + "/" + self.dbName);

Then i created My table class:

class SellsTable(declarative_base()):
    __tablename__ = 'sellers'; #    
    id = Column(Integer,primary_key = True)
    name = Column(String)
    name_type = Column(Integer)
    address = Column(String)
    telephones = Column(String)
    emails = Column(String)
    job_background = Column(String)
    agent_first_name = Column(String)
    agent_last_name = Column(String)    
    agent_attributes = Column(String)
    agent_values = Column(String) 

Its Constructor:

def __init__(self,fieldsName ,fieldsValue):
    completeVariableName = list();
    tmpKeysOfFieldsValue = fieldsValue.keys();
    for fieldsNameCounter in range (0,len(fieldsName)):
        for filedsValueCounter in range(0,len(tmpKeysOfFieldsValue)):
            if fieldsName[fieldsNameCounter] == tmpKeysOfFieldsValue[filedsValueCounter]:
                completeVariableName.append("self." + tmpKeysOfFieldsValue[filedsValueCounter]);
                tmpVariable = completeVariableName[filedsValueCounter];
                tmpValue = fieldsValue[fieldsName[fieldsNameCounter]];
                exec ('%s = \"%s\"' % (tmpVariable, tmpValue));

I have the following function for adding record:

def addRecord(self,tableObj):
    TableClass = tableObj.__class__ ;
    sessionObj = TableClass(['name','telephones'],dict([('name','golrang'),('telephones','092127878,21078288')]));
    self.session.add(sessionObj); ##### MY ERROR LINE 
    self.session.commit();

But when i run add func, i get the following trace back:

Traceback (most recent call last):
  File "./main.py", line 218, in <module>
    main()
  File "./main.py", line 210, in main
    dbObj.addRecord(ddd);
  File "./main.py", line 104, in addRecord
    self.session.commit();
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 721, in commit
    self.transaction.commit()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 354, in commit
    self._prepare_impl()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 334, in _prepare_impl
    self.session.flush()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1818, in flush
    self._flush(objects)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1936, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 58, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1900, in _flush
    flush_context.execute()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
    rec.execute(self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 525, in execute
    uow
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 45, in save_obj
    uowtransaction)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 140, in _organize_states_for_save
    states):
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 772, in _connections_for_states
    base_mapper)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 211, in connection
    bind = self.session.get_bind(bindkey, **kwargs)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1102, in get_bind
    ', '.join(context)))
sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session

回答1:


You should bind engine to your model.

class DbAbsLayer(object):

    def createSession(self):
        Session = sessionmaker()
        self.session = Session.configure(bind=self.engine)


来源:https://stackoverflow.com/questions/18445772/sqlalchemy-exc-unboundexecutionerror-could-not-locate-a-bind-configured-on-mapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!