tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Tortoise unable to fetch instances of models with a postgres.tsvector field

Open S0mbre opened this issue 3 years ago • 3 comments

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: image

image

To Reproduce

  1. 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'})]
  1. 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.

  1. [Optional: add some data]
await EventPartner.create(name='partner')
  1. 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

S0mbre avatar Nov 03 '22 04:11 S0mbre

Hi @S0mbre , it seems this type is not yet implemented -> see issue

nemanjab17 avatar Nov 25 '22 22:11 nemanjab17

Sorry to bother you, but has this been realized already?

inhowo avatar Oct 06 '24 06:10 inhowo

Got the same error in the version 0.25.1. It seems easy to fix, but I'm not familiar with tortoise-orm and something could be missing:

class TSVectorField(Field):
    SQL_TYPE = "TSVECTOR"
    field_type = str

shagi-osoigo avatar Jul 04 '25 10:07 shagi-osoigo