Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Primary keys are always marked as nullable #1227

Open
pmdevita opened this issue Jul 11, 2024 · 3 comments · May be fixed by #1249
Open

[BUG] Primary keys are always marked as nullable #1227

pmdevita opened this issue Jul 11, 2024 · 3 comments · May be fixed by #1249

Comments

@pmdevita
Copy link

pmdevita commented Jul 11, 2024

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

if field.primary_key or blank or null or optional:
default = None
nullable = True

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
Copy link
Author

pmdevita commented Aug 2, 2024

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.

@julienc91
Copy link
Contributor

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

@pmdevita
Copy link
Author

pmdevita commented Aug 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants