1
votes

I am using databases package in my fastapi app. databases has execute and fetch functions, when I tried to return column values after inserting or updating using execute, it returns only the first value, how to get all the values without using fetch..

This is my query

INSERT INTO table (col1, col2, col3, col4) VALUES ( val1, val2, val3, val4 ) RETURNING col1, col2;

3

3 Answers

1
votes

INSERT INTO table (col1, col2, col3, col4) VALUES ( val1, val2, val3, val4 ) RETURNING (col1, col2);

you can use this query to get all columns

1
votes

You can use the query method having parameters of offset = 0 and limit = None to get all the columns

 db.query({YOUR TABLE OBJECT}).offset(0).limit(None).all()

You can refer in this link for more: https://fastapi.tiangolo.com/tutorial/sql-databases/

if You are using mypy then you have to tell the intrepreter that limit value can be None

from typing import Optional
limit: Optional[int] = None
0
votes

I had trouble with this also, this was my query:

INSERT INTO notes (text, completed) VALUES (:text, :completed) RETURNING notes.id, notes.text, notes.completed

Using database.execute(...) will only return the first column.

But.. using database.fetch_one(...) inserts the data and returns all the columns.

Hopes this helps