1 year ago

#378856

test-img

gunnertwin

unhashable type: 'list' FastAPI PUT request

I am attempting to update an entry in my DB which should update the parent table projects as well as the child table rounds but I am getting this error 500:

sqlalchemy.exc.StatementError: (builtins.TypeError) unhashable type: 'list'
[SQL: UPDATE projects SET name = project_name=%(param_1)s, name=%(name)s, total_invested=%(total_invested)s, fee=%(fee)s, total_tokens=%(total_tokens)s, tge_date=%(tge_date)s WHERE projects.id = %(id_1)s]
[parameters: [{}]]

put request:

@router.put("/{id}")
def update_post(
    id: int, updated_project: schemas.ProjectRound, db: Session = Depends(get_db)
):

    project_query = db.query(models.Project).filter(models.Project.id == id)
    project = project_query.first()
    if project is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"project with id {id} not found",
        )

    project_query.update(updated_project.dict(), synchronize_session=False)
    db.commit()
    return project

schemas.py:

class Round(BaseModel):
    round_name: str
    token_price: float
    vesting: str

    class Config:
        orm_mode = True


class ProjectBase(BaseModel):
    name: str
    total_invested: float
    fee: int
    total_tokens: int
    tge_date: str


class ProjectRound(ProjectBase):
    round: List[Round]

    class Config:
        orm_mode = True

models.py:

class Project(Base):
    __tablename__ = "projects"

    id = Column(Integer, primary_key=True, nullable=False)
    name = Column(String, nullable=False, unique=True)
    total_invested = Column(Float, nullable=False)
    fee = Column(Float, nullable=False)
    total_tokens = Column(Integer, nullable=False)
    tge_date = Column(String, nullable=False, server_default="TBC")
    created_at = Column(
        TIMESTAMP(timezone=True), nullable=False, server_default=text("now()")
    )

    round = relationship("Round", back_populates="projects", cascade="all,delete")


class Round(Base):
    __tablename__ = "rounds"

    id = Column(Integer, primary_key=True, nullable=False)
    project_name = Column(
        String, ForeignKey("projects.name", onupdate=("CASCADE"), ondelete=("CASCADE"))
    )
    round_name = Column(String, nullable=False)
    token_price = Column(Float, nullable=False)
    vesting = Column(String, nullable=False)

    projects = relationship("Project", back_populates="round", cascade="all,delete")

I can seem to update the projects and rounds table. This is what I'm sending across from swagger:

{
  "name": "project",
  "total_invested": 0,
  "fee": 0,
  "total_tokens": 0,
  "tge_date": "string",
  "round": [
    {
      "round_name": "test",
      "token_price": 0,
      "vesting": "string"
    },
    {
      "round_name": "test2",
      "token_price": 0,
      "vesting": "string"
    }
  ]
}

python

sqlalchemy

fastapi

pydantic

0 Answers

Your Answer

Accepted video resources