I'm new to fastAPI and trying to build a todo API. I use Tortoise-ORM. I believe the error was caused by entering null value to the todo section although i want it to be null or set to default value which is an empty list. This is the code related to the error. main.py
from typing import List
import os
from fastapi import FastAPI, Depends, HTTPException, status
from models import User, Todo, User_pydantic, UserIn_pydantic, Todo_pydantic, TodoIn_pydantic
from tortoise.contrib.fastapi import register_tortoise, HTTPNotFoundError
app = FastAPI()
@app.post("/users/", response_model=User_pydantic)
async def create_user(user: UserIn_pydantic):
user_obj = await User.create(**user.dict(exclude_unset=True))
return await User_pydantic.from_tortoise_orm(user_obj)
@app.get("/users/", response_model=List[User_pydantic])
async def read_all_users():
return await User_pydantic.from_queryset(User.all())
models.py
from tortoise.models import Model
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
class Todo(Model):
id = fields.IntField(pk=True)
title = fields.CharField(500)
description = fields.TextField(null=True)
Todo_pydantic = pydantic_model_creator(Todo, name="Todo")
TodoIn_pydantic = pydantic_model_creator(Todo, name="TodoIn", exclude_readonly=True)
class User(Model):
id = fields.IntField(pk=True)
username = fields.CharField(500, unique=True)
email = fields.CharField(100, unique=True)
password = fields.CharField(100)
is_active = fields.BooleanField(default=True)
todo: fields.ForeignKeyNullableRelation[Todo] = fields.ForeignKeyField("models.Todo", related_name="todo")
class PydanticMeta:
exclude = ["is_active", "todo"]
User_pydantic = pydantic_model_creator(User, name="User")
UserIn_pydantic = pydantic_model_creator(User, name="UserIn", exclude_readonly=True)
The error began when i tried to create a new user. Here is the error from swagger UI
{
"detail": [
{
"loc": [],
"msg": "null value in column \"todo_id\" of relation \"user\" violates not-null constraint\nDETAIL: Failing row contains (6, bob, [email protected], bob123, t, null).",
"type": "IntegrityError"
}
]
}
The problem is stated in the error
I don't see any
todo_id
column in yourUser
table, but I guess that it's just due to an issue with copying and pasting on SO.I haven't used TortoiseORM (but I would like to once it reaches a stable release), but have you tried changing the definition of todo into the following?
This way you are telling that the field is of type
ForeignKeyNullableRelation
, and eitherTodo
orNone
.That's my guess, too long to fit in the comments, so I posted it as an answer.
Let me know if it works or not.