1

我试图让 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
4

1 回答 1

3

您的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}
于 2012-10-26T01:47:18.157 回答