Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nullable field isn't nullable if also restricted to a list of choices

See original GitHub issue

Issue Description

Describe the bug

Fields with nullable=True aren’t considered nullable when also using choices=...

To Reproduce

Create a model with the following:

from enum import Enum
from ormar import Model, Integer, String
import sqlalchemy
import databases

URL = "postgresql://..."  # url to postgres db

metadata = sqlalchemy.MetaData()
database = database.Database(URL)

class MyChoices(Enum):
    A = "A"
    B = "B"

class Item(Model):
    class Meta:
        metadata = metadata
        database = database

    id = Integer(primary_key=True)
    my_field = String(max_length=1, nullable=True, choices=list(MyChoices))

app = FastAPI()
app.state.database = database

async def startup() -> None:
    database_ = app.state.database
    if not database_.is_connected:
        await database_.connect()

async def shutdown() -> None:
    database_ = app.state.database
    if database_.is_connected:
        await database_.disconnect()"/upload/")
async def upload(item: Item):

Upon trying a POST request to that endpoint, if my_field is set to None, it will fail with a ValueError:

{"detail": [{"loc": ["body", 0, "__root__"], "msg": "my_field: 'None' not in allowed choices set: ['A', 'B']", "type": "value_error"}]}

Expected behavior

The model accepts "A", "B", or None for my_field, because it has a set of valid choices and is nullable. It can be worked around by adding None to the enum, but that does not seem natural to me:

  class MyChoices(Enum):
      A = "A"
      B = "B"
+     NONE = None


  • Database backend used: postgres
  • Python version: 3.9.7
  • ormar version: 0.10.19
  • pydantic version: 1.8.2
  • fastapi version: 0.68.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

classabbyampcommented, Sep 25, 2021

I don’t think it is the same issue. I am only sending it the required/allowed fields, but the choices= restriction on the nullable field isn’t accepting None unless I put None in my list of choices

collerekcommented, Sep 26, 2021

Fixed in 0.10.20

Read more comments on GitHub >

github_iconTop Results From Across the Web

ForeignKey does not allow null values - Stack Overflow
The blank option is used in the form validation, and the null is used when writing to database. So you might add null=True...
Read more >
Understanding null safety - Dart
If you run this Dart program without null safety, it throws a NoSuchMethodError ... The .where() method is lazy, so it returns an...
Read more >
Resolve nullable warnings | Microsoft Learn
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
Model field reference - Django documentation
If a string-based field has null=True , that means it has two possible ... Note that choices can be any sequence object –...
Read more >
Table-level constraints specify the names of the columns to which they apply. ... If the column(s) contain NULL values, the system will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found