django-haystack - Updating index after adding new field to index causing error
Asked Answered
M

2

5

I have a django site which uses Haystack with the Xapian backend for search indexing. I've added a new field to one of the models being indexed, then added that field to the SearchIndex for that model. I've run:

python manage.py update_index

To update the index, but I'm getting the following error:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/management/commands/update_index.py", line 51, in handle
    self.handle_app(None, **options)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/management/commands/update_index.py", line 107, in handle_app
    index.backend.update(index, small_cache_qs[start:end])
  File "/usr/local/lib/python2.6/dist-packages/xapian_haystack-1.1.3beta-py2.6.egg/xapian_backend.py", line 204, in update
    data = index.prepare(obj)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/indexes.py", line 102, in prepare
    self.prepared_data[field_name] = field.prepare(obj)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/fields.py", line 119, in prepare
    return self.convert(super(CharField, self).prepare(obj))
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/fields.py", line 75, in prepare
    raise SearchFieldError("The model '%s' has an empty model_attr '%s' and doesn't allow a default or null value." % (repr(current_object), attr))
haystack.exceptions.SearchFieldError: The model 'None' has an empty model_attr 'address_county' and doesn't allow a default or null value.

The versions I'm using are django 1.2 and django-haystack 1.0.1. Upgrading these to the newest version isn't an option for me at the moment.

Menthol answered 13/12, 2011 at 9:22 Comment(2)
I solved this issue adding null=True in search_indexes for the null field.Syllogize
@Guandalino: I think my issue must have been a bit different from yours then, as you'll see from the answer below that the solution was the oppositeMenthol
M
11

I found the answer. The clue was in the error message (which, as we all know, doesn't always happen!):

The model 'None' has an empty model_attr 'address_county' and doesn't allow a default or null value.

My model field had been created with blank=True, null=True. This caused the error, so I removed those and added default='' and this enabled me to update the index with no error. Hope this helps someone sometime!

Menthol answered 13/12, 2011 at 10:26 Comment(5)
Do you mean you changed your django model? Replacing "blank=True, null=True" with "default=''" in your "address_country" field? Did you again run ./manage.py build_solr_schema after doing this?Chuck
@harisibrahimkv: Yes, I changed my django model. However, I didn't run that management command as I was using Haystack rather than SOLR for my search backend. I did, however, run the equivalent command for Haystack. I just can't remember what that command was anymore.Menthol
Hmm. I tried changing my model and running build_solr_schema again. It did not help. I'm using Haystack as well, but haven't stumbled upon any particular command to be run for this change to reflect.Chuck
@harisibrahimkv: Sorry, I got a bit mixed up there. Although I was using Haystack, the search backend I was using was Xapian. Have you seen django-haystack.readthedocs.org/en/latest/tutorial.html#reindex and tried that to re-index. If you have, or if it doesn't work, I'd recommend asking your own question as I haven't been working with any of these (other than Django) for a couple of years now.Menthol
The preferred method should be marking the corresponding field as null in the index as mentioned at https://mcmap.net/q/2029255/-django-haystack-whoosh-error Remember, a blank ('') and null are not the same things!Mercurialize
F
1

I was facing the same issue when indexing the phone field of my modal. I simply add null=True to the solr field in search_index.py

phone = CharField(model_attr="phone", null=True)

Fatherinlaw answered 2/7, 2019 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.