Will
Will

Reputation: 75625

Multiple joins in a SELECT

I have three tables. These are joined by ForeignKey constraints so sqlalchemy knows how to join them.

I want to select the columns from all three tables:

select([a.c.x, b.c.x, c.c.x], a.c.a.between(10,20), [join(a, c), join(a, b)])

This generates the broken SQL:

SELECT a.x, b.x, c.x
FROM
   a JOIN b ON a.b_id == b.id,
   a JOIN c ON a.c_id == c.id
WHERE
   a.a BETWEEN 10 AND 20;

As can be seen, the table a is in the FROM clause twice!

How can you join three tables in a select() statement using sqlalchemy?

Upvotes: 3

Views: 1048

Answers (2)

MyGGaN
MyGGaN

Reputation: 1806

The short answer is

    select([a.c.x, b.c.x, c.c.x]).\
        select_from(a.join(b).join(c)).\
        where(between(a.c.a, 5, 15))

And if someone want's to try it out here's the whole thing.

    import sqlalchemy
    from sqlalchemy import Table, Column, Integer, String, Sequence,\
        ForeignKey, select, between


    meta = sqlalchemy.MetaData()
    url = 'sqlite:///:memory:'
    engine = sqlalchemy.create_engine(url)


    a = Table(
        'a', meta,
        Column('id', Integer, Sequence('a_id_seq'), primary_key=True),
        Column('age', Integer),
        Column('name', String(20))
    )

    b = Table(
        'b', meta,
        Column('a_id', Integer, ForeignKey("a.id")),
        Column('value', String(20))
    )

    c = Table(
        'c', meta,
        Column('a_id', Integer, ForeignKey("a.id")),
        Column('title', String(20))
    )

    # Create tables
    meta.create_all(engine)

    # Fill with dummy data
    def add_data(age, name, value, title):
        q = a.insert().values({a.c.age: age, a.c.name: name})
        res = engine.execute(q)
        a_id = res.inserted_primary_key[0]

        q = b.insert().values({b.c.a_id: a_id, b.c.value: value})
        engine.execute(q)

        q = c.insert().values({c.c.a_id: a_id, c.c.title: title})
        engine.execute(q)

    add_data(12, 'Foo', 'Bar', 'Baz')
    add_data(17, '111', '222', '333')

    q = select([a.c.name, b.c.value, c.c.title]).\
        select_from(a.join(b).join(c)).\
        where(between(a.c.age, 5, 15))

    print(str(q))
    # SELECT a.name, b.value, c.title
    # FROM a JOIN b ON a.id = b.a_id JOIN c ON a.id = c.a_id
    # WHERE a.age BETWEEN :age_1 AND :age_2

    res = engine.execute(q)
    for row in res.fetchall():
        print(row)
        # ('Foo', 'Bar', 'Baz')

Updated answer, thx for the comment Will!

Upvotes: 1

Lawrence Giordano
Lawrence Giordano

Reputation: 33

give the below a go.

SELECT a.x, b.x, c.x
FROM *TABLENAME* a
JOIN *TABLENAME* b
ON a.id = b.id
JOIN *TABLENAME* c
ON a.id = c.id
WHERE
a.a BETWEEN 10 AND 20

Upvotes: 0

Related Questions