gpt4 book ai didi

python-3.x - SQlAlchemy __tablename__ NameError

转载 作者:行者123 更新时间:2023-12-03 22:44:37 29 4
gpt4 key购买 nike

我对 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: name 'tablename' is not defined



我究竟做错了什么?

最佳答案

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

__tablename__ == 'Collection'

关于python-3.x - SQlAlchemy __tablename__ NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com