Creating a GIN index using SearchVector on JSONB field without casting it to text

I have an index defined like this on my model:


And the queries made against it are like this (hoping to use more ORM in the future, but resorting to raw SQL for now):

SELECT id FROM my_table
WHERE to_tsvector('english', some_jsonfield) @@ websearch_to_tsquery('english', %s)

One would think the index should apply, but I had a suspicion it didn’t.

Turns out, the following SQL is being generated by the migration that adds the above index:

CREATE INDEX "some_jsonfield" ON "my_table"
USING gin ((to_tsvector('english'::regconfig, COALESCE(("some_jsonfield")::text, ''))));

Django seems (?) to think we want to cast it as ::text . However, we query without casting body to text, which makes me believe this index does not end up being used.

It’s possible to create the required index using raw SQL, but I wonder whether I’m missing something or it’s a bug in Django.