Skip to content

Broken relations when SQLAlchemy's schema specified in the metadata for a Base class #58

@lgautier

Description

@lgautier

This message is sent to STDERR

Table 'bar.foo_id' is a foreign key to 'foo' which is not included in the graph, skipping the connection.

and the relation is missing with the following minimal example:

# Generated from a jinja2 template by topomop.
from sqlalchemy.orm import (
    DeclarativeBase,
    Mapped,
    mapped_column
)

from sqlalchemy import (
    Integer,
    Column,
    ForeignKey,
    MetaData
)

schema_name = 'MySchema'
metadata_obj = MetaData(
    schema=schema_name
)


class Base(DeclarativeBase):
    metadata = metadata_obj


class Foo(Base):

    __tablename__ = 'foo'

    id = mapped_column(
        Integer,
        primary_key=True,
        nullable=False,
    )


class Bar(Base):

    __tablename__ = 'bar'

    id = mapped_column(
        Integer,
        primary_key=True,
        nullable=False,
    )

    foo_id = mapped_column(
        Integer,
        ForeignKey('foo.id'),
        nullable=False,
    )

If commenting out the schema argument, the warning message to STDERR is no longer present.

metadata_obj = MetaData(
#     schema=schema_name
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions