django-ninja
django-ninja copied to clipboard
[BUG] Primary keys are always marked as nullable
Describe the bug
As a result of #1181, all primary keys are now marked as nullable in ModelSchema, regardless of whether the field is set to nullable or not. I'm not sure why this was done, but I would expect it to defer to the field's own nullable setting instead if for some reason you have a nullable primary key. If the intention was to make this optional for request schemas, I think it should be marked in fields_optional instead in the Meta class.
This is a result of these lines here in fields.py
https://github.com/vitalik/django-ninja/blob/8a499928dafb68626e1bcf5c231f23933e4ec241/ninja/orm/fields.py#L153-L155
Versions (please complete the following information):
- Python version: 3.12
- Django version: 5.0.7
- Django-Ninja version: 1.2.1
- Pydantic version: 2.8.2
Sorry to ping you @julienc91 but what was the reason you added the primary key to the check here? I'm probably going to make a PR to fix this and add some tests, but I'm wondering if I missed something and if this should actually stay as it is.
@pmdevita I think I misinterpreted the original code, it looks like a bug on my part indeed
Awesome, alright I'll try and get a PR up in a few hours