'Why do I keep getting "TypeError: must be real number, not str" in FastAPI
I am new to FastAPI, and I am still figuring out stuff but I keep getting this error "TypeError: must be real number, not str" and "Internal Server Error" in my responce body for this function. I have been working on this since last night, I thought my get_art_year function looks okay. I am trying to get the table Art and Made_Year values for a specific art_id.
Here is my function:
def get_art_year(db: Session, art_id: int):
a = db.query(models.Art, models.Made_Year).select_from(models.Art).join(models.Art.id_year).filter(models.Art.art_id == art_id)
print(a)
return a.first()
Here is in my main.py:
@app.get("/arts/{art_id}/year", response_model=schemas.Art)
def read_art_year(art_id: int, db: Session = Depends(get_db)):
db_art = attributes.get_art_year(db, art_id=art_id)
if db_art is None:
raise HTTPException(status_code=404, detail="User not found")
return db_art
Here is my models.py:
class Art(Base):
__tablename__ = "ART"
art_id = Column(Integer, primary_key=True)
ascension_year = Column(Numeric(4,0))
art_name = Column(String)
title = Column(String)
culture = Column(String)
artist_name = Column(String)
art_date = Column(String)
medium = Column(String)
country = Column(String)
link_resource = Column(String)
art_wikidata_url = Column(String)
id_year = relationship("Made_Year", back_populates="identification")
class Made_Year(Base):
__tablename__ = "MADE_YEAR"
art_id = Column(Integer, ForeignKey("ART.art_id"), primary_key=True)
art_date = Column(Numeric(4,0))
art_begin_date = Column(Numeric(4,0))
art_end_date = Column(Numeric(4,0))
identification = relationship("Art", back_populates="id_year")
And, schema.py:
class Art(BaseModel):
art_id : int
ascension_year : int
art_name : str
title : str
culture : str
artist_name : str
art_date : str
medium : str
country : str
link_resource : str
art_wikidata_url : str
class Config:
orm_mode = True
class Made_Year(BaseModel):
art_id : int
art_date : str
art_begin_date : int
art_end_date : int
class Config:
orm_mode = True
Here is the full error:
INFO: Started server process [24592]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:56541 - "GET /docs HTTP/1.1" 200 OK
INFO: 127.0.0.1:56541 - "GET /openapi.json HTTP/1.1" 200 OK
SELECT "ART".art_id AS "ART_art_id", "ART".ascension_year AS "ART_ascension_year", "ART".art_name AS "ART_art_name", "ART".title AS "ART_title", "ART".culture AS "ART_culture", "ART".artist_name AS "ART_artist_name", "ART".art_date AS "ART_art_date", "ART".medium AS "ART_medium", "ART".country AS "ART_country", "ART".link_resource AS "ART_link_resource", "ART".art_wikidata_url AS "ART_art_wikidata_url", "MADE_YEAR".art_id AS "MADE_YEAR_art_id", "MADE_YEAR".art_date AS "MADE_YEAR_art_date", "MADE_YEAR".art_begin_date AS "MADE_YEAR_art_begin_date", "MADE_YEAR".art_end_date AS "MADE_YEAR_art_end_date"
FROM "ART" JOIN "MADE_YEAR" ON "ART".art_id = "MADE_YEAR".art_id
WHERE "ART".art_id = ?
C:\Users\trini\Documents\Project 461\.\attributes.py:14: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage.
return a.first()
INFO: 127.0.0.1:56543 - "GET /arts/34/year HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\applications.py", line 261, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
raise e
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 227, in app
raw_response = await run_endpoint_function(
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 162, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\concurrency.py", line 39, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\anyio\to_thread.py", line 28, in run_sync
return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\anyio\_backends\_asyncio.py", line 818, in run_sync_in_worker_thread
return await future
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\anyio\_backends\_asyncio.py", line 754, in run
result = context.run(func, *args)
File "C:\Users\trini\Documents\Project 461\.\main.py", line 53, in read_art_year
db_art = attributes.get_art_year(db, art_id=art_id)
File "C:\Users\trini\Documents\Project 461\.\attributes.py", line 14, in get_art_year
return a.first()
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\query.py", line 2819, in first
return self.limit(1)._iter().first()
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\result.py", line 1102, in first
return self._only_one_row(
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\result.py", line 559, in _only_one_row
row = onerow(hard_close=True)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\result.py", line 1675, in _fetchone_impl
row = next(self.iterator, _NO_ROW)
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\loading.py", line 147, in chunks
fetch = cursor._raw_all_rows()
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\result.py", line 393, in _raw_all_rows
return [make_row(row) for row in rows]
File "C:\Users\trini\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\result.py", line 393, in <listcomp>
return [make_row(row) for row in rows]
TypeError: must be real number, not str
make_row Function:
def make_row(row):
row = _make_row(row)
for fn in fns:
row = fn(row)
return row
Solution 1:[1]
Your art_date definition for Made_Year is not right.
In the models, it defined as Numeric:
art_date = Column(Numeric(4,0))
however, in the schema, it defined as str:
art_date : str
You should use just one of them: Numeric & int or String & str.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
