nullable field isn't nullable if also restricted to a list of choices
See original GitHub issueIssue 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:
tablename="items"
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
@app.on_event("startup")
async def startup() -> None:
database_ = app.state.database
if not database_.is_connected:
await database_.connect()
@app.on_event("shutdown")
async def shutdown() -> None:
database_ = app.state.database
if database_.is_connected:
await database_.disconnect()
@app.post("/upload/")
async def upload(item: Item):
await Item.save()
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
Versions
- Database backend used: postgres
- Python version: 3.9.7
ormar
version: 0.10.19pydantic
version: 1.8.2fastapi
version: 0.68.1
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >CONSTRAINT clause
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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 acceptingNone
unless I putNone
in my list of choicesFixed in 0.10.20