Skip to content

How to add a db schema to this #281

Closed

Description

@lesourcil

Hi normally to add a schema in sqlalchemy i do
Base = declarative_base() Base.metadata.schema = 'schema_name

How can i add this to this stack please ?

Activity

  1. teschmitt commented on Oct 2, 2020

    @teschmitt

    This template has most of the groundwork laid for you and already and includes a User model (along with an authentication flow) and an Item example model. Take a look into the backend/model and backend/schema directories to get started. Plus the docs have got you covered: https://fastapi.tiangolo.com/tutorial/sql-databases/

  2. Andrew-Sheridan commented on Apr 22, 2021

    @Andrew-Sheridan

    @teschmitt I'm not sure your answer here address the question raised by @lesourcil

    If I spin up a stack from this template then during migrations tables get created in the public schema in the DB.

    Altering the db.base_class to have some metadata

    from sqlalchemy import MetaData
    
    @as_declarative(metadata=MetaData(schema='some_schema_name'))
    ...

    should setup the models to be under some_schema_name

    • Though getting alembic to create this schema is another story.
  3. Andrew-Sheridan commented on Apr 22, 2021

    @Andrew-Sheridan

    In the alembic.env file if you update run_migrations() to

    def run_migrations(connection):
        context.configure(connection=connection, target_metadata=target_metadata, compare_type=True,        include_schemas=True,
            version_table_schema="some_schema_name")
    
        connection.execute(text("CREATE SCHEMA IF NOT EXISTS some_schema_name"))
        with context.begin_transaction():
            context.run_migrations()
    • then the schema will be created
  4. locked and limited conversation to collaborators on Sep 2, 2025
  5. converted this issue into a discussion #1826 on Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions