Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema not included in foreign keys in SQLAlchemy model #41

Closed
leppikallio opened this issue Nov 11, 2022 · 4 comments
Closed

Schema not included in foreign keys in SQLAlchemy model #41

leppikallio opened this issue Nov 11, 2022 · 4 comments

Comments

@leppikallio
Copy link
Contributor

In a case where there are multiple schemas, schema name should be included in the generated foreign key references.

As an an example, a really simple dbdiagrams.io "model":

image

The exported PostgreSQL DDL looks like this:

CREATE SCHEMA "schema1";

CREATE SCHEMA "schema2";

CREATE TABLE "schema1"."table1" (
  "id" int PRIMARY KEY,
  "reference_to_table_in_another_schema" int NOT NULL
);

CREATE TABLE "schema2"."table2" (
  "id" int PRIMARY KEY
);

ALTER TABLE "schema1"."table1" ADD FOREIGN KEY ("reference_to_table_in_another_schema") REFERENCES "schema2"."table2" ("id");

Generating sqlalchemy model from this with command

omm /tmp/sample_ddl.sql -m sqlalchemy --no-global-schema

creates model like this:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()


class Table1(Base):

    __tablename__ = "table1"

    id = sa.Column(sa.Integer(), primary_key=True)
    reference_to_table_in_another_schema = sa.Column(
        sa.Integer(), sa.ForeignKey("table2.id"), nullable=False
    )

    __table_args__ = dict(schema="schema1")


class Table2(Base):

    __tablename__ = "table2"

    id = sa.Column(sa.Integer(), primary_key=True)

    __table_args__ = dict(schema="schema2")

Problem is that the schema name is missing from sa.ForeignKey("table2.id"). This will fail because this statement would mean that the table should be found under "public" schema in postgresql, whereas the table is in fact in schema2.

File "/root/.cache/pypoetry/virtualenvs/test-enkIQtTI-py3.9/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 2530, in _resolve_column
    raise exc.NoReferencedTableError(
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'table1.reference_to_table_in_another_schema' could not find table 'table2' with which to generate a foreign key to target column 'id'

Adding the schema reference like below fixes the issue:

    reference_to_table_in_another_schema = sa.Column(
        sa.Integer(), sa.ForeignKey("schema2.table2.id"), nullable=False
@xnuinside
Copy link
Owner

xnuinside commented Nov 20, 2022

@leppikallio are you sure that it must be sa.ForeignKey("table2.id") ? without schema? and not sa.ForeignKey("schema2.table2.id")?

@leppikallio
Copy link
Contributor Author

@leppikallio are you sure that it must be sa.ForeignKey("table2.id") ? without schema? and not sa.ForeignKey("schema2.table2.id")?

@xnuinside, it has to be indeed in format sa.ForeignKey("schema2.table2.id"), in case schema_global = False. If schema_global = True the schema is essentially ignored "everywhere" if it exists in DDL and then also the FK has to be defined like sa.ForeignKey("table2.id")?

@xnuinside
Copy link
Owner

xnuinside commented Nov 20, 2022

@leppikallio I released 0.13.0 with your new feature ) and thanks for your impact and PR! If will be needed anything else - feel free to open new issue or PR

@leppikallio
Copy link
Contributor Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants