-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
-
First Check
- I added a very descriptive title here.
- I used the GitHub search to find a similar question and didn't find it.
- I searched in the documentation/README.
- I already searched in Google "How to do X" and didn't find any information.
- I already read and followed all the tutorial in the docs/README and didn't find an answer.
Commit to Help
- I commit to help with one of those options 👆
Example Code
@computed_field # type: ignore[misc]
@property
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
return MultiHostUrl.build(
scheme="postgresql+psycopg",
username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD,
host=self.POSTGRES_SERVER,
port=self.POSTGRES_PORT,
path=self.POSTGRES_DB,
)
Description
I want to use sqlite instead Pstgresql, can you help me to replace it in the backend.
Operating System
Windows
Operating System Details
windows 10
Python Version
python 3.11
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions
Hi @auh83
Edit SQLALCHEMY_DATABASE_URI with this code
SQLITE_DB_FILE: str = "" @computed_field # type: ignore[prop-decorator] @property def SQLALCHEMY_DATABASE_URI(self) -> str | PostgresDsn: if self.SQLITE_DB_FILE: return f"sqlite:///{self.SQLITE_DB_FILE}" else: return MultiHostUrl.build( scheme="postgresql+psycopg", username=self.POSTGRES_USER, password=self.POSTGRES_PASSWORD, host=self.POSTGRES_SERVER, port=self.POSTGRES_PORT, path=self.POSTGRES_DB, )
and in your .env file SQLITE_DB_FILE=my_db.sqlite
--> python backend\app\initial_data.py
Replies: 2 comments 1 reply
-
@tiangolo I imagine there is a simple way to remplace Postgresql by sqlite !
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
If the creator of FastAPI says so, then there's hope ;-)
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi @auh83
Edit SQLALCHEMY_DATABASE_URI with this code
SQLITE_DB_FILE: str = "" @computed_field # type: ignore[prop-decorator] @property def SQLALCHEMY_DATABASE_URI(self) -> str | PostgresDsn: if self.SQLITE_DB_FILE: return f"sqlite:///{self.SQLITE_DB_FILE}" else: return MultiHostUrl.build( scheme="postgresql+psycopg", username=self.POSTGRES_USER, password=self.POSTGRES_PASSWORD, host=self.POSTGRES_SERVER, port=self.POSTGRES_PORT, path=self.POSTGRES_DB, )
and in your .env file SQLITE_DB_FILE=my_db.sqlite
--> python backend\app\initial_data.py
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 2