Sqlalchemy.exc.UnboundExecutionError:无法找到在mapper上配置的绑定Mapper | SellsTable |卖方或此会话

时间:2013-08-26 13:50:08

标签: python database sqlalchemy mysql-python

我创建了一个用于处理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 个答案:

答案 0 :(得分:12)

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

class DbAbsLayer(object):

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