我正在尝试让 SQLAlchemy 自动加载我的表列(如果重要的话,使用 MySQL)。我有:
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
class User(object):
__tablename__ = 'users'
__table_args__ = {'schema': 'users', 'autoload': True}
这是我的错误:
InvalidRequestError: SQL expression, column, or mapped entity expected - got '<class 'myproject.models.User'>'
我添加了一个 __init__
函数,但仍然得到相同的错误:
def __init__(self,col1,col2):
self.col1 = col1
self.col2 = col2
最佳答案
您的User
类应将Base
作为其父级,而不是object
:
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
engine = create_engine(db_string, echo=db_echo)
Base = declarative_base()
Base.metadata.bind = engine
class User(Base):
__tablename__ = 'users'
__table_args__ = {'schema': 'users', 'autoload': True}
关于python - 如何使用 SQLAlchemy、自动加载和 Pyramid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079653/