SQlAlchemy __tablename__ NameError

时间:2016-03-14 21:56:13

标签: python-3.x sqlalchemy

我对SQlAlchemy完全不熟悉,并试图获得它,我写了以下代码:

from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class Collection(Base):
    __tablename__ == 'Collection'
    id = Column(Integer, primary_key=True)
    name = Column(String)


class Sheet(Base):
    __tablename__ = 'sheet'
    id = Column(Integer, primary_key=True)
    name = Column(String(200), nullable=False)
    collection_id = Column(Integer, ForeignKey('collection.id'))

def main():
    base_name = 'test.db'
    engine = create_engine('sqlite:///{}'.format(base_name))
    session = sessionmaker()
    session.configure(bind=engine)
    Base.metadata.create_all(engine)

if __name__ == "__main__":
    main()

当我运行这个脚本时,我得到:

  

NameError:未定义名称“ tablename

我做错了什么?

1 个答案:

答案 0 :(得分:4)

您在==的定义中有一个比较运算符=而不是赋值运算符Collection

__tablename__ == 'Collection'