Value substitution using encode/databases fecthall method

134 views Asked by At

I'm using encode/databases(https://www.encode.io/databases/database_queries/) which has been configured to connect using postgresql+aiopg://DBUSER:[email protected]:1234/SOMEDB Take the following function:

async def get_table_sample(table_name: str, db: Database):
    result = await db.fetch_all("SELECT * FROM :table_name limit 100", values={"table_name": table_name})
    return result

When ran, throws the following error:

psycopg2.errors.SyntaxError: at or near "rally_iteration": syntax error
DETAIL:  source SQL:
SELECT * FROM 'rally_iteration' limit 100

Notice the quotes around the subbed variable. If I hard code the query to "SELECT * from rally_iteration limit 100" everything works as expected.

Any help is appreciated. Thanks!

1

There are 1 answers

0
NoPlaceLike127.0.0.1 On BEST ANSWER

Variable substitution in the FROM clause is not supported in asyncpg and many other sql libs.