hi folks -

Python debate here w/ SQLAlchemy.

First let me show you what SQLAlchemy's text() construct looks like. You use a plain SQL string, and you can use parameters like :param. so if you have two text() statements together, and they both use the same :param name, then they're going to be the same value in the SQL even if you tried to have them different. this is illustrated in the program shown. the result you will get is (3, 4) since :a and :b are first assigned 1, 2, but later 3 and 4. 1/

from sqlalchemy import text, create_engine, union


e = create_engine("sqlite://", echo="debug")


def my_statement_one():
    a = 1
    b = 2
    return text("select :a, :b").bindparams(a=a, b=b)

def my_statement_two():
    a = 3
    b = 4
    return text("select :a, :b").bindparams(a=a, b=b)

with e.connect() as conn:
    stmt = union(
        my_statement_one().columns(),
        my_statement_two().columns(),
    )

    print(conn.execute(stmt).all())
0

If you have a fediverse account, you can quote this note from your own instance. Search https://hachyderm.io/users/zzzeek/statuses/114506492875494636 on your instance and quote it. (Note that quoting is not supported in Mastodon.)