xnuinside / omymodels

O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table, Models from SQL DDL. And convert one models to another.
MIT License
176 stars 20 forks source link

Include schema in FK reference #42

Closed leppikallio closed 1 year ago

leppikallio commented 1 year ago

a potential way to "solve" the https://github.com/xnuinside/omymodels/issues/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.

xnuinside commented 1 year ago

@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 commented 1 year ago

@leppikallio I fixed code to solve issues in current tests, but can you answer on my question here: https://github.com/xnuinside/omymodels/issues/41 need to add test case to changes that you made

leppikallio commented 1 year ago

@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 commented 1 year ago

@leppikallio I added your test