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?