Search code examples
pythondatabasesqlalchemygino

Add table columns to select without adding it to select_from


I have a prepared function in the database, which I want to call using Gino. This function has a return type equal to one of the tables, that is created using declarative. What I try to do is:

select(MyModel).select_from(func.my_function);

The problem is, that SQLAlchemy automatically detects the table in my select and adds it implicitly to select_from. The resulting SQL contains both my function and the table name in the FROM clause and the result is a cartesian of the function result and the whole table (not what I want really).

My question is – can I somehow specify that I want to select all the columns for a model without having the corresponding class in the FROM?


Solution

  • You have to specify the columns (as an array) if you don't want SA to automatically add MyModel to the FROM clause.

    You have to either do this:

    select([your_model_table.c.column1, your_model_table.c.column2]).select_from(func.my_function);
    

    Your if you want all columns:

    select(your_model_table.columns).select_from(func.my_function);