SQLalchemy issue when extending Pyramid application
-
28-10-2019 - |
Question
I have a "Main Application", and in this application I have the following in the init.py file:
def main(global_config, **settings):
# various config settings
config.include(site_configs)
def site_configs(config):
config.add_route('portfolio', '/portfolio',
view='mainapp.views.portfolio',
view_renderer='/site/portfolio.mako')
And in the views.py I have:
def portfolio(request):
## some code here
project_records = dbsession.query(projects).from_statement(
'SELECT * FROM projects ORDER by id DESC').all()
return {'project_records': project_records}
And then I have a new application, which I want to extend.
So in the init.py I have done:
from mainapp import site_configs
def main(global_config, **settings):
# various config settings
config.include(site_configs)
But when I run this new application, I get the following error (full traceback at the bottom of this message):
UnboundExecutionError: Could not locate a bind configured on mapper
Mapper|projects|projects, SQL expression or this Session
The sqlalchemy engine has been properly set up in both applications.
Also what I want to do is use the database in the new application and not the one in the original main application.
----------------------------
Full Traceback
----------------------------
URL: http://127.0.0.1:6543/portfolio
Module weberror.evalexception:431 in respond view
>> app_iter = self.application(environ, detect_start_response)
Module repoze.tm:23 in __call__ view
>> result = self.application(environ, save_status_and_headers)
Module pyramid.router:158 in __call__ view
>> response = view_callable(context, request)
Module pyramid.config:2824 in _rendered_view view
>> response = wrapped_view(context, request)
Module pyramid.config:2916 in _requestonly_view view
>> response = view(request)
Module mainapp.views:62 in portfolio view
>> project_records = dbsession.query(projects).from_statement('SELECT * FROM projects ORDER by id DESC').all()
Module sqlalchemy.orm.query:1579 in all view
>> return list(self)
Module sqlalchemy.orm.query:1689 in __iter__ view
>> return self._execute_and_instances(context)
Module sqlalchemy.orm.query:1694 in _execute_and_instances view
>> mapper=self._mapper_zero_or_none())
Module sqlalchemy.orm.session:717 in execute view
>> engine = self.get_bind(mapper, clause=clause, **kw)
Module sqlalchemy.orm.session:853 in get_bind view
>> ', '.join(context)))
Solution
Well this really has nothing to do with Pyramid. You have a global variable dbsession
that you are attempting to share between different subapps of your application. If the model is the same for all of your applications then you should just have a single initialization function to configure your global in the main()
. If you have different models for different subapps that's fine, but you really shouldn't try to shove them all into the same global in an overlapping fashion, which is what it sounds like is happening here. Maybe you can be more specific about how you're intending for this to work?