Repository navigation
pathlib.Path is probably an unsupported type #397
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Aug 10, 2022 I don't know about this, but would not be surprising as a Path is not a string, but has also attributes like parents. It can be made from a string though, so that would at least work with the varchar in db.
Searched for the docs and happened to see this, might be a different thing though as is not with db code there. fastapi/fastapi#563
Ah the fix there is to make it jsonable:
"This PR adds a case to jsonable_encoder so that pathlib.PurePath objects are serializable."Other folks here use json db fields for some things I think, we haven't so far but just have strings and ints and such in the db like usually.
Am using strings for paths in the sqlmodel db models. Well or don't have paths but just filenames and paths come from runtime logic.
Any update on this ?
I think I can live with modeling
Pathasstrfor the moment and cast it back toPathwhen I use the data, but something likeField(sa_column=Column(TEXT))would obviously be much more elegant.Can I somehow add the FastAPI fix to my code locally ?
Any update on this ?
I think I can live with modeling
Pathasstrfor the moment and cast it back toPathwhen I use the data, but something likeField(sa_column=Column(TEXT))would obviously be much more elegant.Can I somehow add the FastAPI fix to my code locally ?
You maybe could use a SQLAlchemy Custom Type to convert
Pathto / from string:from pathlib import Path from sqlalchemy.types import TypeDecorator, String class PathType(TypeDecorator): impl = String def process_bind_param(self, value, dialect): if isinstance(value, Path): return str(value) return value def process_result_value(self, value, dialect): if value is not None: return Path(value) return value
And use it like this:
class MyFile(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) path: Path = Field(sa_column=Column(PathType))
Stumbled on this thread by accident while researching a similar issue. Hope it helps!
Reacted by Giovanni Misitano and Nemec- locked and limited conversation to collaborators
on Aug 7, 2025
First Check
Commit to Help
Example Code
Description
pathlib.Path.pathlib.Path.Operating System
Linux
Operating System Details
Ubuntu 22.04.
SQLModel Version
0.0.6
Python Version
3.10
Additional Context
Logs from the test case: