【发布时间】:2011-10-29 03:33:52
【问题描述】:
使用文档中的示例,我有以下代码。当我尝试追加时,我收到错误:
AttributeError: 'NoneType' object has no attribute 'append'
显然,即使不使用append,parent.child 也属于 NoneType。我不知道如何处理这种关系。
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child", backref="parents")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine("mysql://localhost/test", echo=False)
Session = sessionmaker(bind=engine)
session = Session()
metadata = Base.metadata
metadata.drop_all(engine)
metadata.create_all(engine)
parent = Parent()
child = Child()
parent.child.append(child)
【问题讨论】:
标签: python sqlalchemy