Flask-SQLAlchemy模型关系和查询

时间:2019-11-06 11:11:16

标签: python flask sqlalchemy flask-sqlalchemy

我的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)

一个User可以有一个或多个TransactionUser可以有一个或多个InstrumentTransaction可以有一个Instrument。一个RoundTrip可以有一个或多个Transaction

RoundTripTransactionMapping模型只是一个映射表,用于将RoundTrip绑定到一个或多个Transactions

我想用RoundTrip查询所有Instrument.symbol == "GOOG"

如何使用SQLAlchemy的ORM做到这一点?

(我什至还没有正确定义这些关系?对此我有点迷茫。)

1 个答案:

答案 0 :(得分:0)

排序,实际上非常简单。 join上的SQLAlchemy文档保存了这一天。

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