2010-02-04 36 views
3

我不確定我在這裏做錯了什麼來保證這條消息。任何幫助我的配置將不勝感激。SQLAlchemy和掛架錯誤「找不到綁定器上配置的綁定」

"""The application's model objects""" 
import sqlalchemy as sa 
from sqlalchemy import orm 

from project.model import meta 

def now(): 
    return datetime.datetime.now() 

def init_model(engine): 
    """Call me before using any of the tables or classes in the model""" 
    sm = orm.sessionmaker(autoflush=True, autocommit=True, bind=engine) 
    meta.Session.configure(bind=engine) 
    meta.engine = engine 
    meta.Session = orm.scoped_session(sm) 

class User(object): 
    pass 

t_user = sa.Table("User", meta.metadata, 
sa.Column("id", sa.types.Integer, primary_key=True), 
sa.Column("name", sa.types.String(100), nullable=False), 
sa.Column("first_name", sa.types.String(100), nullable=False), 
sa.Column("last_name", sa.types.String(100), nullable=False), 
sa.Column("email", sa.types.String(100), nullable=False), 
sa.Column("password", sa.types.String(32), nullable=False) 
) 

orm.mapper(User,t_user) 

從Python控制檯,我執行:

from project.model import * 

mr_jones = User() 
meta.Session.add(mr_jones) 
mr_jones.name = 'JR Jones' 
meta.Session.commit() 

我收到的錯誤是:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|User|User or this Session 

感謝您的幫助。

回答

3

此問題已解決。我不知道使用從CLI塔的時候,我必須包括整個環境:

from paste.deploy import appconfig 
from pylons import config 

from project.config.environment import load_environment 

conf = appconfig('config:development.ini', relative_to='.') 
load_environment(conf.global_conf, conf.local_conf) 

from project.model import * 

這個沒有問題,執行數據庫查詢後。

+0

如果我想通過'pip'或'distribuition packages'安裝,'project.config.environment'模塊的名稱是什麼? – PersianGulf 2013-08-26 12:38:38