mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Rework the documentation
This commit is contained in:
@@ -22,42 +22,38 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
|
||||
|
||||
## Setup User table
|
||||
|
||||
Let's create a `metadata` object and declare our User table.
|
||||
Let's declare our SQLAlchemy `User` table.
|
||||
|
||||
```py hl_lines="5 32 33"
|
||||
```py hl_lines="13 14"
|
||||
{!./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!
|
||||
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
|
||||
|
||||
We'll now create an SQLAlchemy engine and ask it to create all the defined tables.
|
||||
|
||||
```py hl_lines="36 37 38 39 40"
|
||||
```py hl_lines="17 18 19 20"
|
||||
{!./src/db_sqlalchemy.py!}
|
||||
```
|
||||
|
||||
!!! 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/).
|
||||
|
||||
## Create the database adapter
|
||||
## Create the database adapter dependency
|
||||
|
||||
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
|
||||
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="42 43"
|
||||
```py hl_lines="25 26"
|
||||
{!./src/db_sqlalchemy.py!}
|
||||
```
|
||||
|
||||
Notice that we pass it three things:
|
||||
|
||||
* A reference to your [`UserDB` model](../model.md).
|
||||
* The `users` variable, which is the actual SQLAlchemy table behind the table class.
|
||||
* A reference to your [`UserDB` model](../models.md).
|
||||
* A `database` instance, which allows us to do asynchronous request to the database.
|
||||
|
||||
## Next steps
|
||||
|
||||
We will now configure an [authentication method](../authentication/index.md).
|
||||
* The `users` variable, which is the actual SQLAlchemy table behind the table class.
|
||||
|
||||
## What about SQLAlchemy ORM?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user