SQLAlchemy¶
FastAPI Users provides the necessary tools to work with SQL databases thanks to SQLAlchemy Core and encode/databases package for full async support.
Installation¶
Install the database driver that corresponds to your DBMS:
pip install databases[postgresql]
pip install databases[mysql]
pip install databases[sqlite]
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.
import databases import sqlalchemy from fastapi import FastAPI from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base DATABASE_URL = "sqlite:///./test.db" database = databases.Database(DATABASE_URL) Base: DeclarativeMeta = declarative_base() class UserTable(Base, SQLAlchemyBaseUserTable): pass engine = sqlalchemy.create_engine( DATABASE_URL, connect_args={"check_same_thread": False} ) Base.metadata.create_all(engine) users = UserTable.__table__ user_db = SQLAlchemyUserDatabase(database, users) app = FastAPI() @app.on_event("startup") async def startup(): await database.connect() @app.on_event("shutdown") async def shutdown(): await database.disconnect()
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 enigne and ask it to create all the defined tables.
import databases import sqlalchemy from fastapi import FastAPI from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base DATABASE_URL = "sqlite:///./test.db" database = databases.Database(DATABASE_URL) Base: DeclarativeMeta = declarative_base() class UserTable(Base, SQLAlchemyBaseUserTable): pass engine = sqlalchemy.create_engine( DATABASE_URL, connect_args={"check_same_thread": False} ) Base.metadata.create_all(engine) users = UserTable.__table__ user_db = SQLAlchemyUserDatabase(database, users) app = FastAPI() @app.on_event("startup") async def startup(): await database.connect() @app.on_event("shutdown") async def shutdown(): await database.disconnect()
Tip
In production, you would probably want to create the tables with Alembic, integrated with migrations, etc.
Create the database adapter¶
The database adapter of FastAPI Users makes the link between your database configuration and the users logic. Create it like this.
import databases import sqlalchemy from fastapi import FastAPI from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base DATABASE_URL = "sqlite:///./test.db" database = databases.Database(DATABASE_URL) Base: DeclarativeMeta = declarative_base() class UserTable(Base, SQLAlchemyBaseUserTable): pass engine = sqlalchemy.create_engine( DATABASE_URL, connect_args={"check_same_thread": False} ) Base.metadata.create_all(engine) users = UserTable.__table__ user_db = SQLAlchemyUserDatabase(database, users) app = FastAPI() @app.on_event("startup") async def startup(): await database.connect() @app.on_event("shutdown") async def shutdown(): await database.disconnect()
Notice that we declare the users
variable, which is the actual SQLAlchemy table behind the table class. We also use our database
instance, which allows us to do asynchronous request to the database.
Next steps¶
We will now configure an authentication method.
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.