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

✨ Extend schema json #961

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lamindb_setup/_schema_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class FieldMetadata(BaseModel):
model_name: str
schema_name: str
is_link_table: bool
is_primary_key: bool
relation_type: RelationType | None = None
related_field_name: str | None = None
related_model_name: str | None = None
Expand All @@ -156,19 +157,23 @@ class FieldMetadata(BaseModel):

class _ModelHandler:
def __init__(self, model, module_name: str, included_modules: list[str]) -> None:
from lamindb.models import LinkORM

self.model = model
self.class_name = model.__name__
self.module_name = module_name
self.model_name = model._meta.model_name
self.table_name = model._meta.db_table
self.included_modules = included_modules
self.fields = self._get_fields_metadata(self.model)
self.is_link_table = issubclass(model, LinkORM)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we had this line already since the beginning. Good that we track this now!


def to_dict(self, include_django_objects: bool = True):
_dict = {
"fields": self.fields.copy(),
"class_name": self.class_name,
"table_name": self.table_name,
"is_link_table": self.is_link_table,
}

for field_name in self.fields.keys():
Expand Down Expand Up @@ -242,13 +247,14 @@ def _get_field_metadata(self, model, field: Field):
related_model_name = None
related_schema_name = None
related_field_name = None
field_name = field.name
else:
related_model_name = field.related_model._meta.model_name
related_schema_name = field.related_model.__get_module_name__()
schema_name = field.model.__get_module_name__()
related_field_name = field.remote_field.name
field_name = field.name

field_name = field.name
is_primary_key = getattr(field, "primary_key", False)

if relation_type in ["one-to-many"]:
# For a one-to-many relation, the field belong
Expand Down Expand Up @@ -278,6 +284,7 @@ def _get_field_metadata(self, model, field: Field):
field_name=field_name,
type=internal_type,
is_link_table=issubclass(field.model, LinkORM),
is_primary_key=is_primary_key,
column_name=column,
relation_type=relation_type,
related_schema_name=related_schema_name
Expand Down
7 changes: 7 additions & 0 deletions tests/hub-local/test_update_schema_in_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "artifact",
"schema_name": "core",
"is_link_table": False,
"is_primary_key": True,
"relation_type": None,
"related_field_name": None,
"related_model_name": None,
Expand All @@ -63,6 +64,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "artifact",
"schema_name": "core",
"is_link_table": False,
"is_primary_key": False,
"relation_type": None,
"related_field_name": None,
"related_model_name": None,
Expand All @@ -81,6 +83,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "artifact",
"schema_name": "core",
"is_link_table": False,
"is_primary_key": False,
"relation_type": "many-to-one",
"related_field_name": "created_artifacts",
"related_model_name": "user",
Expand All @@ -99,6 +102,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "gene",
"schema_name": "bionty",
"is_link_table": False,
"is_primary_key": False,
"relation_type": "many-to-many",
"related_field_name": "genes",
"related_model_name": "pathway",
Expand All @@ -117,6 +121,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "well",
"schema_name": "wetlab",
"is_link_table": False,
"is_primary_key": False,
"relation_type": "many-to-many",
"related_field_name": "wells",
"related_model_name": "artifact",
Expand All @@ -135,6 +140,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "transform",
"schema_name": "core",
"is_link_table": False,
"is_primary_key": False,
"relation_type": "many-to-many",
"related_field_name": "successors",
"related_model_name": "transform",
Expand All @@ -153,6 +159,7 @@ def test_update_schema_in_hub(setup_instance):
"model_name": "transform",
"schema_name": "core",
"is_link_table": False,
"is_primary_key": False,
"relation_type": "many-to-many",
"related_field_name": "predecessors",
"related_model_name": "transform",
Expand Down