我使用sqlalchemy orm automap来映射我现有的mysql表。我正在使用三个数据库,在我的项目中的数据库之间存在交叉关系。我有三个DBHandler类。其中一个处理程序如下所示,
class EngineDBHandle(DBHandleBase):
def __init__(self):
self.Base_ = automap_base()
self.engine_ = create_engine('mysql+pymysql://root:xxxxxxx' \
'@localhost/my_db?charset=utf8', \
pool_recycle=3600,encoding='utf-8')
self.Base_.prepare(self.engine_,
reflect=True,
name_for_scalar_relationship=self.name_for_scalar_relationship)
self.session_ = Session(self.engine_)
我创建了表存储库类来访问各个表,例如
class CompanyRepo(BaseRepo):
def __init__(self, dbHandle):
# create a cursor
self.Table_ = dbHandle.Base_.classes.companies
self.session_ = dbHandle.session_
当我尝试使用APScheduler访问我的表类时,有时我的DAL会因以下错误代码而失败,
One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|users|users'. Original exception was: Class 'sqlalchemy.ext.automap.subscription_plan' is not mapped
Job "e4a02f60-7d95-11e7-8314-18a6f713ce75 (trigger: date[2017-08-10 12:05:25 IST], next run at: 2017-08-10 12:05:25 IST)" raised an exception
Traceback (most recent call last):
File "/home/admin1/Clodura 2/clodura/webapp/server/resources/emailcampaign/campaign_process_resource.py", line 236, in collect_campaign_data
email_details = add_update_email_entry(email_details, True)
File "/home/admin1/Clodura 2/clodura/webapp/server/resources/emailcampaign/campaign_process_resource.py", line 333, in add_update_email_entry
email = email_repo.add(email_details,True)
File "/home/admin1/Clodura 2/clodura/dal/base.py", line 59, in add
new_instance = self.Table_(**row)
File "<string>", line 4, in __init__
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/state.py", line 408, in _initialize_instance
manager.dispatch.init(self, args, kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2969, in _event_on_init
configure_mappers()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2850, in configure_mappers
mapper._post_configure_properties()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1765, in _post_configure_properties
prop.init()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/interfaces.py", line 183, in init
self.do_init()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1652, in do_init
self._process_dependent_arguments()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1709, in _process_dependent_arguments
self.target = self.mapper.mapped_table
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 754, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1631, in mapper
configure=False)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 426, in class_mapper
raise exc.UnmappedClassError(class_)
sqlalchemy.orm.exc.UnmappedClassError: Class 'sqlalchemy.ext.automap.subscription_plan' is not mapped
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/admin1/venv/lib/python3.5/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/admin1/Clodura 2/clodura/webapp/server/resources/emailcampaign/campaign_process_resource.py", line 297, in collect_campaign_data
'error_message': str(e)}, True)
File "/home/admin1/Clodura 2/clodura/dal/base.py", line 80, in update_by_id
self.session_.query(self.Table_).filter(
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 1330, in query
return self._query_cls(entities, self, **kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 111, in __init__
self._set_entities(entities)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 122, in _set_entities
self._set_entity_selectables(self._entities)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 152, in _set_entity_selectables
ent.setup_entity(*d[entity])
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 3521, in setup_entity
self._with_polymorphic = ext_info.with_polymorphic_mappers
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 754, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1948, in _with_polymorphic_mappers
configure_mappers()
File "/home/admin1/venv/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2847, in configure_mappers
raise e
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|users|users'. Original exception was: Class 'sqlalchemy.ext.automap.subscription_plan' is not mapped
127.0.0.1 - - [10/Aug/2017 12:05:30] "GET /api/v1/users/14791602-7cdb-11e7-9b3a-18a6f713ce75 HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/admin1/Clodura 2/clodura/webapp/server/resources/user_resource.py", line 37, in get
user_record_cursor = user.read_by_id(userID)
File "/home/admin1/Clodura 2/clodura/dal/base.py", line 33, in read_by_id
Exception)
dal.exceptions.CloduraException: (199, 'Unknown Error', 'An Unkown Error Occured', <class 'Exception'>)
127.0.0.1 - - [10/Aug/2017 12:05:30] "GET /api/v1/campaign/14791602-7cdb-11e7-9b3a-18a6f713ce75/0/undefined HTTP/1.1" 200 -
127.0.0.1 - - [10/Aug/2017 12:05:30] "GET /client/app/css/fonts/OpenSans-Regular.ttf HTTP/1.1" 404 -
127.0.0.1 - - [10/Aug/2017 12:05:30] "GET / HTTP/1.1" 304 -
127.0.0.1 - - [10/Aug/2017 12:05:30] "GET / HTTP/1.1" 304 -
dbHandle.Base_.classes: <sqlalchemy.util._collections.Properties object at 0x7fd597a49b88>
inside get fun in user_resource
exception is === in read by id One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|users|users'. Original exception was: Class 'sqlalchemy.ext.automap.subscription_plan' is not mapped
127.0.0.1 - - [10/Aug/2017 12:05:31] "GET /api/v1/users/14791602-7cdb-11e7-9b3a-18a6f713ce75 HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/admin1/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/admin1/venv/lib/python3.5/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/admin1/Clodura 2/clodura/webapp/server/resources/user_resource.py", line 37, in get
user_record_cursor = user.read_by_id(userID)
File "/home/admin1/Clodura 2/clodura/dal/base.py", line 33, in read_by_id
Exception)
dal.exceptions.CloduraException: (199, 'Unknown Error', 'An Unkown Error Occured', <class 'Exception'>)
我不明白这个问题。请麻我。