Sqlalchemy.exc.UnboundExecutionError:找不到映射器 Mapper|SellsTable|sellers 或此会话上配置的绑定

Posted

技术标签:

【中文标题】Sqlalchemy.exc.UnboundExecutionError:找不到映射器 Mapper|SellsTable|sellers 或此会话上配置的绑定【英文标题】:Sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session 【发布时间】:2013-08-29 00:57:25 【问题描述】:

我创建了一个使用 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);

然后我创建了我的表类:

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) 

它的构造函数:

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));

我有以下添加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();

但是当我运行 add func 时,我得到以下跟踪:

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】:

您应该将引擎绑定到您的模型。

class DbAbsLayer(object):

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

【讨论】:

Session.configure return None,不需要影响到self.session那里。 或者,如果您不在课堂上,请使用以下内容:Session = sessionmaker() engine = create_engine(your_db_connection_string) Session.configure(bind=engine)

以上是关于Sqlalchemy.exc.UnboundExecutionError:找不到映射器 Mapper|SellsTable|sellers 或此会话上配置的绑定的主要内容,如果未能解决你的问题,请参考以下文章