AlexandriaILS / Alexandria

Open Source Integrated Library System
1 stars 2 forks source link

bulk_create() does not call .save() and therefore doesn't update searchable_ fields #50

Open brianporeilly opened 1 year ago

brianporeilly commented 1 year ago

When using bootstrap_dev_site, the bulk_create() method is used when generating Patrons and Items. This means that save() is not called, and therefore the searchable_ fields (like searchable_title and searchable_legal_first_name) are not populated.

brianporeilly commented 1 year ago

I can work on this in theory, but I wanted to open it as an issue to discuss best practices before starting anything.

itsthejoker commented 1 year ago

That's by design on Django's side -- https://docs.djangoproject.com/en/4.1/ref/models/querysets/#bulk-create. It bypasses .save() because it's slow af; the best option here would be to prepopulate those fields before passing them into bulkcreate. Your IDE may complain; if you haven't found them yet, all of the searchable* fields are created / added to the model at runtime over here. https://github.com/AlexandriaILS/Alexandria/blob/master/alexandria/searchablefields/apps.py The whole concept flies in the face of best practices, but it's a clean-ish solution that works (as long as .save() isn't getting bypassed).

brianpatrickoreilly commented 1 year ago

Yeah, I agree it's preferred to not mess with the actual code hwre. More just an issue with the seed script, it's not obvious why search doesn't work if you use it to seed your local dev env.

itsthejoker commented 1 year ago

It probably wouldn't be too much of a timesink to just call for obj in Record.objects.all(): obj.save(). Worth testing.

brianpatrickoreilly commented 1 year ago

That's basically what I did after the seed script ran to get things working. It didn't take very long.