Skip to content

Instantly share code, notes, and snippets.

@b4oshany
Created March 24, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4oshany/fec076a49d083caa7efb8ef3b8ac9608 to your computer and use it in GitHub Desktop.
Save b4oshany/fec076a49d083caa7efb8ef3b8ac9608 to your computer and use it in GitHub Desktop.
Kotti self join
from kotti.resources import Content
from sqlalchemy.orm import aliased
parent_table = aliased(Content)
# Perform a self join with a where query, which adds the
# following line::
# left join contents as n on contents.id = n.id
# where n.title like '%<title>%'
query = query.outerjoin(
parent_table, parent_table.id == Content.id
).filter(parent_table.title.ilike(
"%{}%".format(parent_title)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment