django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Primary keys are always marked as nullable

Open pmdevita opened this issue 1 year ago • 3 comments

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

pmdevita avatar Jul 11 '24 14:07 pmdevita

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 avatar Aug 02 '24 14:08 pmdevita

@pmdevita I think I misinterpreted the original code, it looks like a bug on my part indeed

julienc91 avatar Aug 02 '24 18:08 julienc91

Awesome, alright I'll try and get a PR up in a few hours

pmdevita avatar Aug 02 '24 18:08 pmdevita