Nick Humrich
Nick Humrich

Reputation: 15755

Sqlalchemy Core, insert statement returning * (all columns)

I am using sqlalchemy core (query builder) to do an insert using a table definition. For example:

table.insert().values(a,b,c)

and I can make it return specific columns:

table.insert().values(a,b,c).returning(table.c.id, table.c.name)

but I am using postgres which has a RETURNING * syntax, which returns all the columns in the row. Is there a way to do that with sqlalchemy core?

Upvotes: 12

Views: 12190

Answers (3)

ryche
ryche

Reputation: 2094

Alternatively, you can expand table columns:

table.insert().returning(*table.c).values(a,b,c)

Upvotes: 3

cubanguy
cubanguy

Reputation: 141

To get all table columns, one can also do the following query:

table.insert().values(a,b,c).returning(table)

Upvotes: 14

user3437231
user3437231

Reputation: 292

query = table.insert().values(a,b,c).returning(literal_column('*'))

And you can access it like

for col in execute(query, stmt):
   print(col)

Upvotes: 17

Related Questions