将 SearchVectorField 添加到 Django 中的模型
Adding a SearchVectorField to a model in Django
所以我正在尝试向 Django 中的模型添加 SearchVectorField:
class JobPosting(models.Model):
...
...
search_vector = SearchVectorField()
我知道它应该是 nullable
或具有能够迁移的默认值,因此我删除了 table 中的所有条目以防止出现此问题。
但是,当 运行 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:
如果 table 是空的,为什么会这样说?我不想让该列可以为空,如果可以避免的话,我宁愿没有默认值。
我的问题是,有什么办法可以强制 makemigrations
和 migrate
,因为如果 table 为空,我不明白这个问题。我还有其他 table 包含我不想删除的数据,因此无法删除数据库中的所有信息。
或者,如果选项 1)
是解决方案,我将如何格式化此类字段的默认值?我认为这不是普通的文本字段?
感谢您的帮助。
我不完全确定您为什么不想使用默认值,但我会假设这是给定的。
My question is, is there any way to force makemigrations and migrate
as I don't understand the problem if the table is empty.
您的当前 数据库table 可能是空的,但迁移应该在其他数据库实例上重复table。因此 Django 不能假设同样适用于任何其他数据库。
解决方法可能是定义一个迁移,该迁移将字段创建为可为 null,为所有条目编制索引,然后将其更新为不可为 null。
# -*- 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),
),
]
我只是让该字段可以为空(并且可能不可编辑,因为您不会在管理界面或通过表单更改它):
class JobPosting(models.Model):
...
...
search_vector = SearchVectorField(null=True, editable=False)
迁移不会有任何问题。
稍后您可以使该字段不可为空,但没有真正的理由这样做,因为无论如何您都将以编程方式更新它。
所以我正在尝试向 Django 中的模型添加 SearchVectorField:
class JobPosting(models.Model):
...
...
search_vector = SearchVectorField()
我知道它应该是 nullable
或具有能够迁移的默认值,因此我删除了 table 中的所有条目以防止出现此问题。
但是,当 运行 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:
如果 table 是空的,为什么会这样说?我不想让该列可以为空,如果可以避免的话,我宁愿没有默认值。
我的问题是,有什么办法可以强制 makemigrations
和 migrate
,因为如果 table 为空,我不明白这个问题。我还有其他 table 包含我不想删除的数据,因此无法删除数据库中的所有信息。
或者,如果选项 1)
是解决方案,我将如何格式化此类字段的默认值?我认为这不是普通的文本字段?
感谢您的帮助。
我不完全确定您为什么不想使用默认值,但我会假设这是给定的。
My question is, is there any way to force makemigrations and migrate as I don't understand the problem if the table is empty.
您的当前 数据库table 可能是空的,但迁移应该在其他数据库实例上重复table。因此 Django 不能假设同样适用于任何其他数据库。
解决方法可能是定义一个迁移,该迁移将字段创建为可为 null,为所有条目编制索引,然后将其更新为不可为 null。
# -*- 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),
),
]
我只是让该字段可以为空(并且可能不可编辑,因为您不会在管理界面或通过表单更改它):
class JobPosting(models.Model):
...
...
search_vector = SearchVectorField(null=True, editable=False)
迁移不会有任何问题。
稍后您可以使该字段不可为空,但没有真正的理由这样做,因为无论如何您都将以编程方式更新它。