Decorator that sets all `SQLModel` fields to `Optional`
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google “How to X in SQLModel” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
import sqlmodel
class HeroBase(sqlmodel.SQLModel):
name: str = sqlmodel.Field(index=True)
secret_name: str
age: Optional[int] = sqlmodel.Field(default=None, index=True)
team_id: Optional[int] = sqlmodel.Field(
default=None, foreign_key="team.id"
)
class HeroUpdate(sqlmodel.SQLModel):
name: Optional[str] = None
secret_name: Optional[str] = None
age: Optional[int] = None
team_id: Optional[int] = None
Description
It feels bad to define every field manually to Optional
. (Also prompt to error)
Wanted Solution
It would be better to have some kind of decorator or something that allows to to this at runtime
Wanted Code
import sqlmodel
class HeroBase(sqlmodel.SQLModel):
name: str = sqlmodel.Field(index=True)
secret_name: str
age: Optional[int] = sqlmodel.Field(default=None, index=True)
team_id: Optional[int] = sqlmodel.Field(
default=None, foreign_key="team.id"
)
@sqlmodel.all_fields_to_optional
class HeroUpdate(HeroBase):
pass
Alternatives
No response
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.8
Python Version
Python 3.10.6
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Convert all fields to optional #64 - tiangolo/sqlmodel - GitHub
Set all base fields to Optional based on keyword argument. Wanted Code. class HeroBase(SQLModel): name: str secret_name: str ...
Read more >Read Data - SELECT - SQLModel
For example, in our database, we only have one table that has all the columns, id , name , secret_name , age ....
Read more >python - Different behavior Field in pydantic and SQLModel in ...
To my knowledge, SQLmodel is partially based on pydantic . ... Optional from fastapi import FastAPI from sqlmodel import Field, SQLModel app ...
Read more >Custom Types - SQLAlchemy 1.4 Documentation
Custom Types¶. A variety of methods exist to redefine the behavior of existing types as well as to provide new ones.
Read more >Models - pydantic
Initialisation of the object will perform all parsing and validation, ... from typing import List, Optional from pydantic import BaseModel class ...
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
This usecase is pretty common across a few projects using Pydantic, and it has come up a few times, but it has been postponed until Pydantic version 2. Because of that, implementing it in SQLModel itself doesn’t seem like the best approach (as this could also be requested for FastAPI, or Starlite, etc…).
Instead, it might be worth commenting your support for this on existing issues https://github.com/pydantic/pydantic/pull/3179, or requesting it as a feature for Pydantic version 2: https://github.com/pydantic/pydantic/discussions/categories/pydantic-v2
Until v2 comes out, there are a few decent solutions:
The metaclass solution from Stackoverflow posted by Drdilyor looks quite nice and should accomplish what you want:
Thanks for the discussion and for coming back to close it! ☕
For completeness, yep, the thing is that it’s actually part of how the Python language works. This would probably be a feature request to Python itself. A type defined as a string is indeed a different type then the union of a string and None. Those two are different in several ways.
And supporting something like this would mean that your code, your editor, tools, would be thinking that your code means something, checking for some specific errors, giving you autocompletion for some specific things, and it would all be wrong. The intention of SQLModel is to make it easy to get all those features correctly, and to be able to be sure that your code is correct, at least in terms of that.
The other thing is that JSON Schema and OpenAPI would also be broken, and if you have automatically generated clients or developers using your OpenAPI (e.g. Swagger in
/docs
) they would all have incorrect information, invalid types. Which is even worse than no types.