sqlalchemy: AttributeError: type object 'customer' has no attribute 'invoices'
sqlalchemy: AttributeError: type object 'customer' has no attribute 'invoices'
我是 sqlalchemy 的新手。我可以像这样通过声明映射创建数据库表:
engine = create_engine("--engine works---")
Base = declarative_base()
class Customer(Base):
__tablename__ = 'customer'
customer_id = Column(Integer, primary_key=True)
name = Column(String(30))
email = Column(String(30))
invoices = relationship(
'Invoice',
order_by="Invoice.inv_id",
back_populates='customer',
cascade="all, delete, delete-orphan"
)
class Invoice(Base):
__tablename__ = 'invoice'
inv_id = Column(Integer, primary_key=True)
name = Column(String(30))
created = Column(Date)
customer_id = Column(ForeignKey('customer.customer_id'))
customer = relationship('Customer', back_populates='invoices')
Base.metadata.create_all(engine)
这很好。我在 customer
和 invoice
表中添加了一些数据。
到目前为止一切顺利。接下来,我会像这样在这个现有数据库上尝试 automap_base
:
from sqlalchemy import select, text
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.ext.automap import automap_base
engine = create_engine('--engine works---')
Base = automap_base()
# reflect
Base.prepare(engine, reflect=True)
Customer = Base.classes.customer
Invoice = Base.classes.invoice
Session = sessionmaker(bind=engine, future=True)
session = Session()
# query invoice
stmt = select(Customer, Invoice).join(Customer.invoices).order_by(Customer.customer_id, Invoice.inv_id)
res = session.execute(stmt)
for c in res:
print(c.customer_id)
当我运行代码时,我得到:
AttributeError: type object 'customer' has no attribute 'invoices'
在这种情况下,Customer
(一侧)或Invoice
(多侧)的关系我错过了什么,所以当我查询客户及其发票属性和发票时与客户属性?感谢您的帮助。
通过 default,automap 将通过在 lower-cased 类名后附加“_collection”来在父级中创建关系,因此名称将为 Customer.invoice_collection
.
在回答这个问题时,我发现连接会在 Customer.invoice_collection
上引发 AttributeError
,除非我事先对 Customer
执行查询,例如
session.execute(sa.select(Customer).where(False))
我不确定为什么会这样,但是您不一定需要连接,因为您可以直接遍历 Customer.invoice_collection
,或者根据发票连接 table:
stmt = sa.select(Customer, Invoice).join(Invoice)
res = session.execute(stmt)
for c, i in res:
print(c.customer_id, i.inv_id)
我是 sqlalchemy 的新手。我可以像这样通过声明映射创建数据库表:
engine = create_engine("--engine works---")
Base = declarative_base()
class Customer(Base):
__tablename__ = 'customer'
customer_id = Column(Integer, primary_key=True)
name = Column(String(30))
email = Column(String(30))
invoices = relationship(
'Invoice',
order_by="Invoice.inv_id",
back_populates='customer',
cascade="all, delete, delete-orphan"
)
class Invoice(Base):
__tablename__ = 'invoice'
inv_id = Column(Integer, primary_key=True)
name = Column(String(30))
created = Column(Date)
customer_id = Column(ForeignKey('customer.customer_id'))
customer = relationship('Customer', back_populates='invoices')
Base.metadata.create_all(engine)
这很好。我在 customer
和 invoice
表中添加了一些数据。
到目前为止一切顺利。接下来,我会像这样在这个现有数据库上尝试 automap_base
:
from sqlalchemy import select, text
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.ext.automap import automap_base
engine = create_engine('--engine works---')
Base = automap_base()
# reflect
Base.prepare(engine, reflect=True)
Customer = Base.classes.customer
Invoice = Base.classes.invoice
Session = sessionmaker(bind=engine, future=True)
session = Session()
# query invoice
stmt = select(Customer, Invoice).join(Customer.invoices).order_by(Customer.customer_id, Invoice.inv_id)
res = session.execute(stmt)
for c in res:
print(c.customer_id)
当我运行代码时,我得到:
AttributeError: type object 'customer' has no attribute 'invoices'
在这种情况下,Customer
(一侧)或Invoice
(多侧)的关系我错过了什么,所以当我查询客户及其发票属性和发票时与客户属性?感谢您的帮助。
通过 default,automap 将通过在 lower-cased 类名后附加“_collection”来在父级中创建关系,因此名称将为 Customer.invoice_collection
.
在回答这个问题时,我发现连接会在 Customer.invoice_collection
上引发 AttributeError
,除非我事先对 Customer
执行查询,例如
session.execute(sa.select(Customer).where(False))
我不确定为什么会这样,但是您不一定需要连接,因为您可以直接遍历 Customer.invoice_collection
,或者根据发票连接 table:
stmt = sa.select(Customer, Invoice).join(Invoice)
res = session.execute(stmt)
for c, i in res:
print(c.customer_id, i.inv_id)