I must be misunderstanding the usage of Metadata in SQLAlchemy... I'm trying to define a data model in one repository and then instantiate that model in a different repository using Alembic. The following is a minimal example:

# test.py from uuid import uuid4, UUID from sqlalchemy import MetaData from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column class Base(DeclarativeBase): metadata = MetaData() class Test(Base): __tablename__ = "test" id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)

And then:

import test as t print(t.Base.metadata.schema) # expected to be `None` print(t.Base.metadata.tables) # Dict of tables with key = 'test' t.Base.metadata.schema = 'new_schema' print(t.Base.metadata.schema) # correctly set as 'new_schema' print(t.Base.metadata.tables) # Keys not updated to be 'new_schema.test'

In my real example, I'm setting the Metadata schema in Alembic's env.py, but Alembic checks both Metadata.tables and Metadata.sorted_tables, neither of which auto-update with the schema. Thanks in advance for your help!

Ferrick4's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.