tortoise / tortoise/tortoise-orm
Tortoise unable to fetch instances of models with a postgres.tsvector field
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
When the DB contains models with at least one field of type tortoise.contrib.postgres.fields.TSVectorField, the instance cannot be fetched by the ORM producing the following exception:
File "C:\_PROG_\WPy64-31020\python-3.10.2.amd64\lib\site-packages\tortoise\queryset.py", line 1006, in _execute
instance_list = await self._db.executor_class(
File "C:\_PROG_\WPy64-31020\python-3.10.2.amd64\lib\site-packages\tortoise\backends\base\executor.py", line 138, in execute_select
instance: "Model" = self.model._init_from_db(
File "C:\_PROG_\WPy64-31020\python-3.10.2.amd64\lib\site-packages\tortoise\models.py", line 738, in _init_from_db
None if value is None else field.field_type(value),
TypeError: 'NoneType' object is not callable
The instance is present in the DB with the tsvector field as expected:


To Reproduce
- Create a Model with a Postgres tsvector field, e.g.
from tortoise import fields
from tortoise.models import Model
from tortoise.contrib.postgres.indexes import GinIndex
from tortoise.contrib.postgres.fields import TSVectorField
class EventPartner(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=256,)
logo = fields.TextField(default='')
tsearch = TSVectorField(null=True, default=None) # the TSVector field for full-text search
class Meta:
ordering = ['name']
indexes = [GinIndex(fields={'tsearch'})]
- Init the ORM and generate the schema as usual:
await Tortoise.init(config=TORTOISE_CONFIG)
await Tortoise.generate_schemas()
At this stage, there is no problem -- the DB is created as expected.
- [Optional: add some data]
await EventPartner.create(name='partner')
- Now try fetching the first (or any) instance of the model:
await db.EventPartner.first()
# or like this
await db.EventPartner.all()
# or with any query
await db.EventPartner.get_or_none(name__iexact='partner')
You will get the error: TypeError: 'NoneType' object is not callable.
Expected behavior
I'd expect the ORM to fetch instances in the usual manner. In the latter example, the first added EventPartner object ought to be retrieved.
Additional context
Python = 3.10.2
Python package versions:
- asyncpg==0.26.0
- python-rapidjson==1.9
- tortoise-orm==0.19.2
PostgreSQL DB / server version 14.2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tortoise/models.py around _init_from_db and follow the fetch path through tortoise/queryset.py and backends/base/executor.py. Reproduce the EventPartner example with a nullable TSVectorField, then verify that first(), all(), and get_or_none() fetch instances without the TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100