Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Adding a SearchVectorField to a model in Django

So I'm trying to add a SearchVectorField to a model in Django:

class JobPosting(models.Model):
    ...
    ...
    search_vector = SearchVectorField()

I get that it should either be nullable or have a default value to be able to migrate so I deleted all entries in the table to prevent this problem.

However, I'm getting the following error when running makemigrations:

You are trying to add a non-`nullable` field 'search_vector' to jobposting without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
   1) Provide a one-off default now
      (will be set on all existing rows with a null value for this column)
   2) Quit, and let me add a default in models.py
Select an option:

Why is it saying this if the table is empty? I don't want to make the column nullable, and I'd rather not have a default value if I can avoid it.

My question is, is there any way to force makemigrations and migrate as I don't understand the problem if the table is empty. I have other tables with data in them which I don't want to delete so can't delete all info in the database.

Alternatively, if option 1) is the solution, how would I format a default value for this type of field? I assume it's not a normal text field?

Thanks for any help.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I am not entirely sure why you do not want to have a default value, but I will assume this as given.

My question is, is there any way to force makemigrations and migrate as I don't understand the problem if the table is empty.

Your current database table might be empty, but migrations are supposed to be repeatable on other database instances. Therefore Django cannot assume that the same holds on any other database.

A workaround might be to do define a migration that creates the field as nullable, indexes all entries and then updates it to be non-nullable.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.postgres.search import SearchVector, SearchVectorField      
from django.db import migrations


def index_entries(apps, schema_editor):
    Entry = apps.get_model("mymodel", "Entry")
    Entry.objects.update(search_vector=SearchVector('body_text'))


class Migration(migrations.Migration):

    dependencies = [
        ('mymodel', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='entry',
            name='search_vector',
            field=SearchVectorField(null=True),
        ),

        migrations.RunPython(index_entries),

        migrations.AlterField(
            model_name='entry',
            name='search_vector',
            field=SearchVectorField(null=False),
        ),
    ]
about 4 years ago · Santiago Trujillo Relatório

0

I'd just make the field nullable (and probably not editable, since you're not going to change it in admin interface nor via forms):

class JobPosting(models.Model):
    ...
    ...
    search_vector = SearchVectorField(null=True, editable=False)

And there will be no issues with migration.

Later you can make this field not nullable, but there is no real reason to do this because you will update it programmatically anyway.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda