Need help creating an SQLAlchemy query + subquery

This is SQL, I need SQLAlchemy to generate through my ORM.

SELECT
    *
FROM
    notes
WHERE
    notes.student_id == {student_id} 
  OR
    notes.student_id IN (
        SELECT
            *
        FROM
            peers
        WHERE
            peers.student_id == {student_id}
          AND
            peers.date_peer_saved >= notes.date_note_saved
    )

SQL not verified. I just wrote it to demonstrate that I need SQLAlchemy.

Basically, a registered student should see a list of saved ones notes. However, the student should only see those that were sent by themselves or those that were sent by one of their peers. But only those peers whom they "were friends" after the note was saved.

This way, the student will not see notes sent by another student before they become peers.

I am having trouble getting this to run in SQLAlchemy ORM. Any help?

+5
2

, .subquery(), , .

-

subq = sess.query(Peers.id).filter(and_(Peers.student==student, XXX)).subquery()

notes = sess.query(Notes).filter(or_(Notes.student==student, Notes.studing.in_(subq))).all()

( , , ).

+8

, , - .

, , date_peer_saved is >= date_note_saved.

, notes peers, , student. , peers, .

, peers notes. , , .

, MAX date_peer_saved >= MAX date_note_saved?

0

All Articles