I have something like the following...

class TimestampMixin(object):
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    modified_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, 
onupdate=datetime.datetime.utcnow)


class Memo(db.Model, TimestampMixin):
    __tablename__ = "memos"
    pk = db.Column(db.Integer, primary_key=True)
    note = db.Column(db.Text)
    date = db.Column(db.Date)
    purpose = db.Column(db.Text)
    product = db.Column(db.Text)
    location = db.Column(db.Text)
    private = db.Column(db.Boolean, default=False, nullable=True)
    in_person = db.Column(db.Boolean, default=False, nullable=True)
    rr = db.Column(db.Boolean, default=False, nullable=True)

    customer_pk = db.Column(db.Integer, db.ForeignKey("customers.pk"))
    author_pk = db.Column(db.Integer, db.ForeignKey("users.pk"))
    contact_pk = db.Column(db.Integer, db.ForeignKey("contacts.pk"))
    r_by_pk = db.Column(db.Integer, db.ForeignKey("users.pk"))

    recipients = db.relationship("MemoRecipient", backref="memo", 
cascade="save-update, 
merge, delete",)
    comments = db.relationship("MemoComment", backref="memo", 
cascade="save-update, 
merge, delete",)
    reminders = db.relationship("MemoReminder", backref="memo", 
cascade="save-update, 
merge, delete",)


class MemoRecipient(db.Model):
    __tablename__ = "memorecipients"
    pk = db.Column(db.Integer, primary_key=True)

    memo_pk = db.Column(db.Integer, db.ForeignKey("memos.pk"))
    user_pk = db.Column(db.Integer, db.ForeignKey("users.pk"))



Whenever a Memo is created, it has an author and optionally gets tagged 
with other users using the MemoRecipient table.

I am trying to figure out how to write a query that will give me all the 
Memos that were authored by (Memo.author_pk) or sent to 
(MemoRecipient.user_pk) a specific user. I also need to order the results 
by Memo.modified_at and paginate the results. I'm thinking I need some type 
of JOIN, I'm just not sure which.

Any advice?

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/e46936a6-84e7-43df-bbf8-8d86385ed8b7o%40googlegroups.com.

Reply via email to