NotNullViolationError for user_id in FastAPI with SQLModel on INSERT despite setting value

28 Views Asked by At

I'm encountering a NotNullViolationError in my FastAPI application using SQLModel when attempting to insert a new record into database. Despite explicitly setting the user_id in my endpoint, the error suggests it's being inserted as null.

Endpoint:

@app.post("/api/interest/", response_model=InterestRead, status_code=status.HTTP_201_CREATED)
async def create_interest(*, interest_create: InterestCreate, session: AsyncSession = Depends(get_session),
                          user_id: uuid.UUID = Depends(user_id_from_header)):
    user = await session.get(User, user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")

    db_interest = Interest.validate(Interest(**interest_create.dict(), user_id=user_id))
    session.add(db_interest)
    await session.commit()
    await session.refresh(db_interest)
    return db_interest

Models:

import uuid
from datetime import datetime, timezone
from typing import List, Optional

import sqlalchemy as sa
from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint


class UserBase(SQLModel):
    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    phone_number: str = Field(max_length=255)
    phone_prefix: str = Field(max_length=10)


class User(UserBase, table=True):
    __table_args__ = (
        UniqueConstraint("phone_number", "phone_prefix", name="phone_numbe_phone_prefix_constraint"),
    )
    registered_at: datetime = Field(sa_column=sa.Column(sa.DateTime(timezone=True), nullable=False),
                                    default_factory=lambda: datetime.now(timezone.utc))
    interests: List["Interest"] = Relationship(back_populates="user")


class UserRead(UserBase):
    pass


class UserCreate(UserBase):
    pass


class InterestBase(SQLModel):
    topic: str = Field(max_length=100)
    found: int = 0
    search: bool = Field(default=False)


class Interest(InterestBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: uuid.UUID = Field(foreign_key="user.id")
    created_at: datetime = Field(sa_column=sa.Column(sa.DateTime(timezone=True), nullable=False),
                                 default_factory=lambda: datetime.now(timezone.utc))

    user: User = Relationship(back_populates="interests")
    proposals: List["Proposal"] = Relationship(back_populates="interest")


class InterestCreate(InterestBase):
    pass


class InterestRead(InterestBase):
    pass

Error:

asyncpg.exceptions.NotNullViolationError: null value in column "user_id" of relation "interest" violates not-null constraint
DETAIL:  Failing row contains (Sample Topic, 1, t, 2, null, 2024-02-29 20:16:29.067898+00).

The user_id is retrieved from request headers and seems to be correctly passed into the Interest instance creation. The user exists in the DB, as validated by the session query. Yet, the insert operation fails with a NotNullViolationError for user_id.

Has anyone faced a similar issue or can spot what might be going wrong here? How can I ensure the user_id is correctly recognized and inserted into the database?

0

There are 0 best solutions below