Skip to content

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

Open
@pmdevita

Description

@pmdevita

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions