SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

user442920

I have an Entries model that has tags, so every entry can have many tags. Tags are a model with a value of tag which is the tag string. I am trying to filter my returned entries on sets of tags. For instance, given two tags, a and b, I want to return only the entries that have both tag a and tag b.

Right now I have OR, in the sense that I can return any entries that have either a or b and I have done it this way.

entObjs = Entries.query.join(Entries.tags).filter(Tags.tag.in_(tagList)).all()

tagList is a list of strings. How do I implement the AND, so I get only entries that have all the tags?

J.J. Hakala

I did something similar using aliased tables and joins,

from sqlalchemy.orm import aliased

def test(self, tags):       
    q = self.session.query(models.Item).\
             join(models.ItemTag).\
             join(models.Tag.\
             filter(models.Tag.name == tags[0])
    i = 0
    for tag in tags[1:]:
        alias1 = aliased(models.Tag)
        alias2 = aliased(models.ItemTag)
        q = q.join(alias2, models.Item.id == alias2.item_id).\
              filter(alias1.id == alias2.tag_id).\
              filter(alias1.name == tag)
        i += 1

    print(str(q))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql performance - finding matching set in one to many table

From Dev

get all record with number of one to many table count

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

filter sqlalchemy joined query, constructed on parsed input

From Dev

SQL Join Constraint Column on Another Joined Table

From Dev

Fluent NHibernate Join mapping to Joined table

From Dev

Arel: join table, sort on joined field

From Dev

Getting two values from same Joined table

From Dev

One to many table creation

From Dev

Update joined table via SQLAlchemy ORM using session.query

From Dev

Self join on joined table

From Dev

SQL: filter a joined table

From Dev

Mysql - search in "one to many" table

From Dev

join values in sqlalchemy

From Dev

join table with another joined table

From Dev

get all record with number of one to many table count

From Dev

Entity Splitting For One-To-Many table relationships

From Dev

MySQL return Null = 0 on joined table with filter on one table

From Dev

sql query join with condition on joined table

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

filter sqlalchemy joined query, constructed on parsed input

From Dev

Hbase One-To-Many -Table Load process

From Dev

Get Max(id) from one to many table

From Dev

join with count on a joined table with group clause in mysql

From Dev

"If one-to-many table has value" as column in SELECT

From Dev

SQL: filter a joined table

From Dev

Mysql - search in "one to many" table

From Dev

How to make a query where a one to many table's result is put into columns

From Dev

Postgres select from one to many table to single table rows

Related Related

  1. 1

    sql performance - finding matching set in one to many table

  2. 2

    get all record with number of one to many table count

  3. 3

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  4. 4

    filter sqlalchemy joined query, constructed on parsed input

  5. 5

    SQL Join Constraint Column on Another Joined Table

  6. 6

    Fluent NHibernate Join mapping to Joined table

  7. 7

    Arel: join table, sort on joined field

  8. 8

    Getting two values from same Joined table

  9. 9

    One to many table creation

  10. 10

    Update joined table via SQLAlchemy ORM using session.query

  11. 11

    Self join on joined table

  12. 12

    SQL: filter a joined table

  13. 13

    Mysql - search in "one to many" table

  14. 14

    join values in sqlalchemy

  15. 15

    join table with another joined table

  16. 16

    get all record with number of one to many table count

  17. 17

    Entity Splitting For One-To-Many table relationships

  18. 18

    MySQL return Null = 0 on joined table with filter on one table

  19. 19

    sql query join with condition on joined table

  20. 20

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  21. 21

    filter sqlalchemy joined query, constructed on parsed input

  22. 22

    Hbase One-To-Many -Table Load process

  23. 23

    Get Max(id) from one to many table

  24. 24

    join with count on a joined table with group clause in mysql

  25. 25

    "If one-to-many table has value" as column in SELECT

  26. 26

    SQL: filter a joined table

  27. 27

    Mysql - search in "one to many" table

  28. 28

    How to make a query where a one to many table's result is put into columns

  29. 29

    Postgres select from one to many table to single table rows

HotTag

Archive