python - SQLAlchemy 关系之间的差异

标签 python sql sqlalchemy

作为 SQLAlchemy 和 SQL 的新手,SQLAlchemy 中的关系让我感到困惑。

这是一套SQLAlchemy模型定义(基于One-To-Many in the official docs)

class Invoice(Base):
    __tablename__ = 'invoices'
    id = Column(Integer, primary_key=True)
    customer = relationship("Customer")

class Customer(Base):
    __tablename__ = 'customers'
    id = Column(Integer, primary_key=True)
    invoice_id = Column(Integer, ForeignKey('invoices.id'))

相同
class Invoice(Base):
    __tablename__ = 'invoices'
    id = Column(Integer, primary_key=True)

class Customer(Base):
    __tablename__ = 'customers'
    id = Column(Integer, primary_key=True)
    invoice_id = Column(Integer, ForeignKey('invoices.id'))
    invoice = relationship("Invoice")

我一直在使用第二种方法定义我的模型,到目前为止它似乎工作得很好。但是我不确定这是否是建立关系的正确方法。

两者有什么区别?

最佳答案

就生成的数据库表和列而言,每个模型都是相同的。区别在于您如何使用 relationship()指令使 ORM 能够与这些表之间的外键关系进行交互。我应该指出,如果您不需要/希望 ORM 提供与表之间的这些关系交互的额外帮助,则根本不需要创建这些关系属性。

在第一个示例中,您正在为 Invoice 创建一个 customer 属性,该属性使您能够执行诸如访问与特定发票关联的任何和所有客户之类的操作。例如,您可以打印与特定发票关联的每个客户 ID。

invoice = session.query(Invoice).filter(Invoice.id == 1).first()
for c in invoice.customer:
    print(c.id)

在第二个示例中,您将为 Customer 创建一个 invoice 属性,该属性使您能够执行诸如访问与特定客户关联的发票数据之类的操作。例如,您可以打印客户的发票 ID(如果您还有其他 Invoice 列尚未被 Customer 中的外键引用,这会更有用)。

customer = session.query(Customer).filter(Customer.id == 1).first()
print(customer.invoice.id)

如果您想访问关系双方的这些属性,以便可以通过上述两种方式(以及其他方式)使用 ORM,您可以使用 back_populatesbackref 参数来连接这两个关系。您可以通过 Linking Relationships with Backref 了解有关这些选项的更多信息。 .

class Invoice(Base):
    __tablename__ = 'invoices'
    id = Column(Integer, primary_key=True)
    customers = relationship("Customer", back_populates="invoice")

class Customer(Base):
    __tablename__ = 'customers'
    id = Column(Integer, primary_key=True)
    invoice_id = Column(Integer, ForeignKey('invoices.id'))
    invoice = relationship("Invoice", back_populates="customers") 

关于python - SQLAlchemy 关系之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58327872/

相关文章:

mysql - 如何编写通过外键的 SQL 查询

python - 使用 key 对身份验证的 Snowflake 的 SQLAlchemy engine.connect() 失败

python - Pygame - 在两点之间移动矩形

python - 从 ipywidgets FileUpload 访问多个上传文件的内容

sql - MySql:它有类似 namespace 的 SCHEMA 吗?

sql - 优化 postgres 查询

python - 为什么我的 python 模拟时钟的指针显示错误的时间?

python - 引用链、引用者链的示例

python - 创建并附加到 SQLAlchemy 数据库表中的列表

python - sqlalchemy.exc.ArgumentError : Can't load plugin: sqlalchemy. 方言:驱动程序