简体   繁体   English

Flask-SQLAlchemy Model 关系和查询

[英]Flask-SQLAlchemy Model Relationships and Query

I have the following SQLAlchemy models in my Flask app:我的 Flask 应用程序中有以下 SQLAlchemy 模型:

class User(UserMixin, db.Model):
    __tablename__ = "users"

    # relationships
    instruments = db.relationship(
        Instrument, backref="instruments", passive_deletes=True, lazy="dynamic"
    )
    transactions = db.relationship(
        Transaction, backref="transactions", passive_deletes=True, lazy="dynamic"
    )
    round_trips = db.relationship(
        RoundTrip, backref="round_trips", passive_deletes=True, lazy="dynamic"
    )

    # columns
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(24), unique=True, index=True)

class Instrument(db.Model):
    __tablename__ = "instruments"

    # relationships
    user = db.relationship("User")
    transaction = db.relationship(
        Transaction, uselist=False, backref="transaction", passive_deletes=True
    )

    # columns
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    symbol = db.Column(db.String(100), index=True)

class Transaction(db.Model):
    __tablename__ = "transactions"

    # relationships
    user = db.relationship("User")
    instrument = db.relationship("Instrument")
    transaction_round_trip_mapping = db.relationship(
        "RoundTripTransactionMapping",
        backref="round_trip_mappings",
        passive_deletes=True,
        lazy="dynamic",
    )

    # columns
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    instrument_id = db.Column(
        db.Integer,
        db.ForeignKey("instruments.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    amount = db.Column(db.Float, nullable=False)

class RoundTrip(ResourceMixin, db.Model):
    __tablename__ = "round_trips"

    # relationships
    user = db.relationship("User")
    transaction_round_trip_mapping = db.relationship(
        "RoundTripTransactionMapping", backref="transaction_mappings", passive_deletes=True, lazy="dynamic"
    )

    # columns
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )

class RoundTripTransactionMapping(db.Model):
    __tablename__ = "round_trip_transaction_mappings"

    # relationships
    round_trip = db.relationship("RoundTrip")
    transaction = db.relationship("Transaction")

    # columns
    id = db.Column(db.Integer, primary_key=True)
    round_trip_id = db.Column(
        db.Integer,
        db.ForeignKey("round_trips.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    transaction_id = db.Column(
        db.Integer,
        db.ForeignKey("transactions.id", onupdate="CASCADE", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    closing_transaction = db.Column(db.Boolean, nullable=False)

A User can have one or more Transaction s.一个User可以有一个或多个Transaction A User can have one or more Instrument s.一个User可以有一个或多个Instrument A Transaction can have one Instrument .一个Transaction可以有一个Instrument A RoundTrip can have one or more Transaction s. RoundTrip可以有一个或多个Transaction

The RoundTripTransactionMapping model is simply a mapping table to tie a RoundTrip to one or more Transactions . RoundTripTransactionMapping model 只是一个映射表,用于将RoundTrip绑定到一个或多个Transactions

I want to query for all RoundTrip s with Instrument.symbol == "GOOG" .我想用Instrument.symbol == "GOOG"查询所有RoundTrip

How can I do this using SQLAlchemy's ORM?如何使用 SQLAlchemy 的 ORM 做到这一点?

(Have I even defined these relationships correctly? I'm getting a bit lost with this.) (我是否正确定义了这些关系?我对此有点迷失了。)

Sorted it, quite simple actually.整理了一下,其实很简单。 The SQLAlchemy docs on join saved the day. join时的 SQLAlchemy 文档挽救了这一天。

result = (
    db.session.query(RoundTrip)
    .join(RoundTripTransactionMapping)
    .join(Transaction)
    .join(Instrument)
    .filter(Instrument.symbol == "GOOG")
    .all()
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM