tortoise / tortoise/tortoise-orm
Auto ID for CockroachDB breaks Pydantic
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
If one tries to use cockroachdb along with pydantic_model_creator() breaks when you try to convert a model object using Object.from_tortoise_orm(tortoise_object)
This error is encountered:
Apr 3 05:32:11 PM return super().from_orm(obj)
Apr 3 05:32:11 PM ^^^^^^^^^^^^^^^^^^^^^
Apr 3 05:32:11 PM File "pydantic/main.py", line 579, in pydantic.main.BaseModel.from_orm
Apr 3 05:32:11 PM pydantic.error_wrappers.ValidationError: 1 validation error for User
Apr 3 05:32:11 PM id
Apr 3 05:32:11 PM ensure this value is less than or equal to 2147483647 (type=value_error.number.not_le; limit_value=2147483647)
To Reproduce
Set up a Model that uses CockroachDB.
class User(models.Model):
"""
The User model
"""
id = fields.IntField(pk=True)
#: This is a email
email = fields.CharField(max_length=50, unique=True)
name = fields.CharField(max_length=50, null=True)
password_hash = fields.CharField(max_length=128, null=True)
created_at = fields.DatetimeField(auto_now_add=True)
modified_at = fields.DatetimeField(auto_now=True)
class Meta:
table = "users"
ordering = ["name"]
class PydanticMeta:
exclude = ["password_hash"]
async def check_password(self, password: str) -> bool:
return bcrypt.checkpw(
password.encode("utf-8"), self.password_hash.encode("utf-8")
)
# Pydantic models
User_Pydantic = pydantic_model_creator(User, name="User")
And then in a code try to create and return the User_Pydantic model :
@auth_router.post("/signup")
async def signup(user: UserRequest):
await user.hash_password() # Hash the password
user_obj = await User.create(**user.dict(exclude_unset=True))
print(user_obj)
return await User_Pydantic.from_tortoise_orm(user_obj)
Expected behavior
Expected behaviour would return the User_Pydantic value in response.
Additional context
Using this config:
[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.95.0"
openai = "^0.27.2"
uvicorn = "^0.21.1"
orjson = "^3.8.9"
python-multipart = "^0.0.6"
asyncpg = "^0.27.0"
psycopg2-binary = "^2.9.5"
fastapi-login = "^1.9.0"
tortoise-orm = {extras = ["asyncpg"], version = "^0.19.3"}
bcrypt = "^4.0.1"
pydantic = {extras = ["email"], version = "^1.10.7"}
aerich = "0.6.3"
[tool.poetry.scripts]
serve = "tuteai_backend:serve_dev"
serve-prod = "tuteai_backend:serve_prod"
[tool.poetry.group.dev.dependencies]
devtools = "^0.10.0"
alembic = "^1.10.2"
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the CockroachDB model example and trace pydantic_model_creator() through User_Pydantic.from_tortoise_orm(user_obj). Compare the generated id validation with the value returned by CockroachDB; done means the converted Pydantic model accepts a newly created object without the integer-bound validation error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100