Errata - CNLearn FastAPI 3 - Alembic Migrations for the Users Table.

So you know how we creates our Users table last time? Well, I had a typo…Today we will fix our migration. The problem is that I called our table user instead of users. user is a restricted keyword in PostgreSQL so nothing actually got created..Oops, my bad. So how dowe fix it?

So how do we fix it? Well, remember how we created both upgrade and downgrade functions? First, we call the downgrade one.

alembic downgrade base

That undid any changes we made. Then, let’s modify our revision file to have the following:

def upgrade():
    op.create_table(
        "users",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("full_name", sa.String(), nullable=True),
        sa.Column("email", sa.String(), nullable=True),
        sa.Column("hashed_password", sa.String(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.Column("is_superuser", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_email"), "users", ["email"], unique=True)
    op.create_index(op.f("ix_user_full_name"), "users", ["full_name"], unique=False)
    op.create_index(op.f("ix_user_id"), "users", ["id"], unique=False)



def downgrade():
    op.drop_index(op.f("ix_user_id"), table_name="users")
    op.drop_index(op.f("ix_user_full_name"), table_name="users")
    op.drop_index(op.f("ix_user_email"), table_name="users")
    op.drop_table("users")

And then we run the migration.

alembic upgrade head

And there you go! That’s all we need to do and the reason why it’s important to have a way to make and undo changes to your database. Thank you Alembic.