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

Include schema in FK reference #42

Merged
merged 4 commits into from
Nov 20, 2022

Conversation

leppikallio
Copy link
Contributor

a potential way to "solve" the #41. It does the trick but perhaps it is not the most elegant way to accomplish the task.

Also, the tests do need some attention.

…a, if the ref_schewa != None

    The "solution" is not that elegant as of now, having two distinct templates, with / without schema, is not of course that pretty way to handle this
@xnuinside
Copy link
Owner

@leppikallio millions of thanks for opening the PR! I really didn't have a time on weekend ( sorry. Can you add test for feature something like here: https://github.com/xnuinside/omymodels/blob/main/tests/functional/generator/test_pydantic_models.py like expected output on some input? And also existed tests failed :) you can just run them on local env with 'pytest tests' no need any specific preparations

@xnuinside
Copy link
Owner

@leppikallio I fixed code to solve issues in current tests, but can you answer on my question here: #41 need to add test case to changes that you made

@leppikallio
Copy link
Contributor Author

leppikallio commented Nov 20, 2022

@leppikallio I fixed code to solve issues in current tests, but can you answer on my question here: #41 need to add test case to changes that you made

@xnuinside , I struggle to make the assert_result == expected work, I simply fail to point out why 🤨, but the principle is correct:

def test_foreign_keys_in_different_schema():
    expected = """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('schema2.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")
            )

"""
    ddl = """
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");
"""
    result = create_models(ddl, schema_global=False, models_type="sqlalchemy")["code"]
    assert result == expected

image

@xnuinside
Copy link
Owner

@leppikallio I added your test

@xnuinside xnuinside merged commit 0840b5d into xnuinside:main Nov 20, 2022
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

Successfully merging this pull request may close these issues.

2 participants