tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.5k stars 368 forks source link

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

Open S0mbre opened 1 year ago

S0mbre commented 1 year ago

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

2. Init the ORM and generate the schema as usual:
```python
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')
  2. 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
nemanjab17 commented 1 year ago

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