Running into a problem using Supabase / RLS / Python / SQLAlchemy

Hello !

I've just begun using Supabase and RLS, a feature that I find very convenient .. but I'm running into a problem and despite searching all over the place.. I don't know how to fix it.

I'm using Python, FastAPI and SQLAlchemy.

My first problem which I've solved was passing via Postgres the proper user authenticated role.. This can be done with :

def configure_session(
db
: Session, 
user_id
: str):

db
.execute(text("SET ROLE authenticated"))

db
.execute(text("SELECT set_config('request.jwt.claim.sub', :user_id, true)"), {"user_id": 
user_id
})

My second problem, not solved as of now is this error message :

Error adding XXXX: Could not refresh instance '<Location at 0x7f8ad5162950>'

Basically, I'm inserting a row in a table , my code is similar to this :

        db.add(location)
        db.commit()
        db.refresh(location)
        return location

If I disable RLS, it works.

If I have RLS turned on, and I select the location, I'll see it.

Is my problem more on RLS's side or SQLAlchemy or .. something else?

thanks a lot!