django-pydantic-field icon indicating copy to clipboard operation
django-pydantic-field copied to clipboard

ValidationError when trying to read bad objects in db

Open mihow opened this issue 2 years ago • 1 comments

Is there a way to disable validation on retrieval? Currently Django will throw a 500 error if I have an object with an invalid schema in the database. Even when calling Model.objects.all().delete() throws a ValidationError. Thanks for any ideas!.

mihow avatar Nov 10 '23 05:11 mihow

Hey @mihow, I think the easiest workaround here is to use extended schema to support what's missing from the actual data model. This way you'll be able to load and cleanup invalid data (or even simply delete it). Another benefit in this case that your schema will be more close to the reality.

Another approach, which involves migrations, could be to temporarily switch the model's field to plain JSONField, perform some cleanup, then switch back to the SchemaField. AFAIK this should be a cheap operation (without actual database schema migration).

The third one, is to execute cleanup SQL directly, either one-time manual run, or during migrations with migrations.RunSQL. This is the most powerful (and most dangerous) way of mitigation.

Hope this helps!

surenkov avatar Nov 10 '23 07:11 surenkov