mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Update doc for SQLALchemy2 adapter
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
# SQLAlchemy
|
||||
|
||||
**FastAPI Users** provides the necessary tools to work with SQL databases thanks to [SQLAlchemy Core](https://docs.sqlalchemy.org/en/13/core/) and [encode/databases](https://www.encode.io/databases/) package for full async support.
|
||||
**FastAPI Users** provides the necessary tools to work with SQL databases thanks to [SQLAlchemy ORM with asyncio](https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html).
|
||||
|
||||
## Installation
|
||||
!!! warning
|
||||
The previous adapter using `encode/databases` is now deprecated but can still be installed using `fastapi-users[sqlalchemy]`.
|
||||
|
||||
Install the database driver that corresponds to your DBMS:
|
||||
## Asynchronous driver
|
||||
|
||||
```sh
|
||||
pip install 'databases[postgresql]'
|
||||
```
|
||||
To work with your DBMS, you'll need to install the corresponding asyncio driver. The common choices are:
|
||||
|
||||
```sh
|
||||
pip install 'databases[mysql]'
|
||||
```
|
||||
|
||||
```sh
|
||||
pip install 'databases[sqlite]'
|
||||
```
|
||||
* For PostgreSQL: `pip install asyncpg`
|
||||
* For SQLite: `pip install aiosqlite`
|
||||
|
||||
For the sake of this tutorial from now on, we'll use a simple SQLite databse.
|
||||
|
||||
@@ -24,20 +18,22 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
|
||||
|
||||
Let's declare our SQLAlchemy `User` table.
|
||||
|
||||
```py hl_lines="13 14"
|
||||
```py hl_lines="15 16"
|
||||
--8<-- "docs/src/db_sqlalchemy.py"
|
||||
```
|
||||
|
||||
As you can see, **FastAPI Users** provides a mixin that will include base fields for our `User` table. You can of course add you own fields there to fit to your needs!
|
||||
|
||||
## Create the tables
|
||||
## Implement a function to create the tables
|
||||
|
||||
We'll now create an SQLAlchemy engine and ask it to create all the defined tables.
|
||||
We'll now create an utility function to create all the defined tables.
|
||||
|
||||
```py hl_lines="17 18 19 20"
|
||||
```py hl_lines="23-25"
|
||||
--8<-- "docs/src/db_sqlalchemy.py"
|
||||
```
|
||||
|
||||
This function can be called, for example, during the initialization of your FastAPI app.
|
||||
|
||||
!!! warning
|
||||
In production, it's strongly recommended to setup a migration system to update your SQL schemas. See [Alembic](https://alembic.sqlalchemy.org/en/latest/).
|
||||
|
||||
@@ -45,16 +41,14 @@ We'll now create an SQLAlchemy engine and ask it to create all the defined table
|
||||
|
||||
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. It should be generated by a FastAPI dependency.
|
||||
|
||||
```py hl_lines="25 26"
|
||||
```py hl_lines="28-34"
|
||||
--8<-- "docs/src/db_sqlalchemy.py"
|
||||
```
|
||||
|
||||
Notice that we pass it three things:
|
||||
Notice that we define first a `get_async_session` dependency returning us a fresh SQLAlchemy session to interact with the database.
|
||||
|
||||
It's then used inside the `get_user_db` dependency to generate our adapter. Notice that we pass it three things:
|
||||
|
||||
* A reference to your [`UserDB` model](../models.md).
|
||||
* A `database` instance, which allows us to do asynchronous request to the database.
|
||||
* The `users` variable, which is the actual SQLAlchemy table behind the table class.
|
||||
|
||||
## What about SQLAlchemy ORM?
|
||||
|
||||
The primary objective was to use pure async approach as much as possible. However, we understand that ORM is convenient and useful for many developers. If this feature becomes very demanded, we will add a database adapter for SQLAlchemy ORM.
|
||||
* The `session` instance we just injected.
|
||||
* The `UserTable` variable, which is the actual SQLAlchemy model.
|
||||
|
||||
Reference in New Issue
Block a user