Reputation: 489
I've added GIN index to my db
ALTER TABLE mtn_order
ADD COLUMN textsearchable_index_col tsvector
GENERATED ALWAYS AS (to_tsvector('english', coalesce(descr, '') || ' ' || coalesce(descrrep, ''))) STORED;
CREATE INDEX textsearch_idx ON mtn_order USING GIN (textsearchable_index_col);
and textsearchable_index_col = SearchVectorField(null=True)
to my model and now when i'm trying to save new instance i get:
ProgrammingError at /order/create/
cannot insert into column "textsearchable_index_col"
DETAIL: Column "textsearchable_index_col" is a generated column.
How to stop Django trying to write None
to that field
Upvotes: 8
Views: 2589
Reputation: 1079
Override the _do_insert and _do_update methods on the model:
class MTNOrder:
def _do_insert(self, manager, using, fields, update_pk, raw):
fields = [
f for f in fields if f.attname not in ['textsearchable_index_col']
]
return super()._do_insert(manager, using, fields, update_pk, raw)
def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
values = [
value for value in values if value[0].attname not in ['textsearchable_index_col']
]
return super()._do_update(base_qs, using, pk_val, values, update_fields, forced_update)
Reference: make django not insert certain fields
Upvotes: 0
Reputation: 35619
You won't be able to add it as a field to your model, because Django will try to write values back to it.
You could annotate it on when you need to:
MyModel.objects.annotate(
index_col=RawSQL('textsearchable_index_col',
[],
output_field=SearchVectorField()
)
Upvotes: 5