databases icon indicating copy to clipboard operation
databases copied to clipboard

How can search_path be changed?

Open gee-shibasaki opened this issue 3 years ago • 1 comments

databases:0.5.5 I am using Postgres, and trying to change search_path.

import asyncio
async def method():
    await database.connect()
    await database.execute(query="SET search_path TO my_schema, public;")
    path = await database.execute(query="show search_path;")
    print(path) #"$user", public
    await database.disconnect()

asyncio.run(method())

I made this code , but search_path doesn't seem to work. Could you tell me the better way to change search_path with databases??

gee-shibasaki avatar May 06 '22 04:05 gee-shibasaki

It seems that when not using a transaction (or using the same connection explictly) each database.execute() returns the real connection to the pool. Forcing to use the same connection using solves the problem:

import asyncio
from databases import Database

async def method():
    database = Database('postgresql://.../...')
    breakpoint()
    await database.connect()
    async with database.connection():  # <- ensures that both execute() use the same connection
        await database.execute(query="SET search_path TO my_schema, public;")
        path = await database.execute(query="show search_path;")
    print(path) #"my_schema", public
    await database.disconnect()

asyncio.run(method())

masipcat avatar Jul 15 '22 14:07 masipcat