2011-09-10 28 views
3

我有一個「主應用程序」,並在此應用程序,我已經中初始化 .py文件如下:SQLAlchemy的問題時,擴展應用金字塔

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

而在views.py我有:

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} 

然後我有一個新的應用程序,我想擴展。

所以在初始化的.py我做:

from mainapp import site_configs 

def main(global_config, **settings): 
    # various config settings 
    config.include(site_configs) 

但是當我運行這個新的應用程序,我得到以下錯誤(在此消息的底部完全回溯):

UnboundExecutionError: Could not locate a bind configured on mapper 
Mapper|projects|projects, SQL expression or this Session 

sqlalchemy引擎已在兩個應用程序中正確設置。

另外我想要做的是在新的應用程序中使用數據庫,而不是原始主應用程序中的數據庫。

---------------------------- 
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))) 
+0

我已通過在新應用程序__init__.py文件中更改此問題來解決該問題:從mainapp.models導入initialize_sql因此我使用主應用程序sqla配置。但有沒有 這樣做,特別是如果我將有 負載使用它的應用程序。謝謝。 – sidewinder

回答

2

那麼這真的和金字塔無關。您有一個全局變量dbsession,您試圖在您的應用程序的不同子應用程序之間共享。如果所有應用程序的模型都是相同的,那麼您應該只有一個初始化函數來配置您的全局在main()。如果你對不同的子應用程序有不同的模型,那很好,但是你真的不應該試圖將它們全部推到同一個全局中,這就是這裏發生的事情。也許你可以更具體地說明你打算如何工作?