I've got a wiki db layout with Page and Revisions. Each Revision includes a page_id referencing the Page, a page relationship towards the recommended page each Page includes a all_revisions relationship to any or all its revisions. To date so common.

But I wish to implement different epochs for that pages: If your page was erased and it is recreated, the brand new revisions possess a new epoch. To assist find the appropriate revisions, each page includes a current_epoch area. Now I wish to give a revisions relation around the page that only consists of its revisions, only individuals in which the epochs match.

This is exactly what I have attempted:

revisions = relationship('Revision',
    primaryjoin = and_(
        'Page.id == Revision.page_id',
        'Page.current_epoch == Revision.epoch',
    ),
    foreign_keys=['Page.id', 'Page.current_epoch']
)

Full code (you might run that because it is)

However this always boosts ArgumentError: Couldn't determine relationship direction for primaryjoin condition ...`, I have attempted all I'd spring to mind, it did not work.

What shall we be held doing wrong? Is a poor method for carrying this out, how can it be done apart from having a relationship?

Try setting up relationship after both courses are produced:

Page.revisions = relationship(
    'Revision',
    primaryjoin = (Page.id==Revision.page_id) & \
                    (Page.current_epoch==Revision.epoch),
    foreign_keys=[Page.id, Page.current_epoch],
    uselist=True,
)

BTW, your test isn't correct: revisions property loads data from database when you haven't added these to session.

Update: The issue inside your code is the fact that primaryjoin parameter isn't string, so it's not evaluated. Using string in primaryjoin works fine:

class Page(Base):
    # [skipped]
    revisions = relationship(
        'Revision',
        primaryjoin = '(Page.id==Revision.page_id) & '\
                        '(Page.current_epoch==Revision.epoch)',
        foreign_keys=[id, current_epoch],
        uselist=True,
    )