You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromhashidsimportHashidsfromsqlmodelimportSQLModel# Initialize Hashids with a salt and minimum lengthdefget_hashids():
returnHashids(salt='some_salt_string', min_length=10)
defencode_id(value) ->str:
"""Encodes an integer to a short string."""returnget_hashids().encode(value)
defdecode_id(encoded_value):
"""Decodes the encoded string back to the original integer."""decoded=get_hashids().decode(encoded_value)
returndecoded[0] ifdecodedelseNoneclassHeroData(SQLModel): # Or HeroBasename: strsecret_name: strclassHero(HeroData, table=True):
id: int|None=Field(default=None, primary_key=True)
classHeroPublic(HeroData):
id_hash: strasyncdefget_hero(hero_id_hash: str):
hero_id=decode_id(hero_id_hash)
hero=session.exec(select(Hero, hero_id))
returnHeroPublic(id_hash) =encode_id(hero.id), **hero.model_dump())
# Outputs:
{
"id_hash": "48rnm2d8",
"name": "Chuck Francine Yavier",
"secret_name": "Professor Y"
}
Description
FastAPI + SQLModel
Given that items in the database often are incremented by 1 for each new item, the item id sequencing can be quite predictable to the public and this may not be something we want to expose.
So instead, we use a hashed id that one can encode/decode using a salted key.
I've been using this workaround for some of my projects and I'm wondering if this is a feature that should be out-of-the-box, especially when using SQLModel with FastAPI. This would also hopefully lead to a more simplified model when working with the HeroPublic model.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
FastAPI + SQLModel
Given that items in the database often are incremented by 1 for each new item, the item id sequencing can be quite predictable to the public and this may not be something we want to expose.
So instead, we use a hashed id that one can encode/decode using a salted key.
I've been using this workaround for some of my projects and I'm wondering if this is a feature that should be out-of-the-box, especially when using SQLModel with FastAPI. This would also hopefully lead to a more simplified model when working with the HeroPublic model.
Operating System
Linux, Windows
Operating System Details
WSL
SQLModel Version
0.0.18
Python Version
3.10.5
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions