Skip to content

Commit 01e2a9d

Browse files
author
matmoncon
committed
fix: add missing index/constraint to model schema
1 parent 03d740f commit 01e2a9d

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

pyneo4j_ogm/core/base.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
from pyneo4j_ogm.fields.relationship_property import RelationshipProperty
3535
from pyneo4j_ogm.fields.settings import BaseModelSettings
3636
from pyneo4j_ogm.logger import logger
37-
from pyneo4j_ogm.pydantic_utils import IS_PYDANTIC_V2, get_model_dump
37+
from pyneo4j_ogm.pydantic_utils import (
38+
IS_PYDANTIC_V2,
39+
get_field_type,
40+
get_model_dump,
41+
get_model_fields,
42+
)
3843
from pyneo4j_ogm.queries.query_builder import QueryBuilder
3944

4045
if TYPE_CHECKING:
@@ -48,6 +53,7 @@
4853

4954
if IS_PYDANTIC_V2:
5055
from pydantic import SerializationInfo, model_serializer, model_validator
56+
from pydantic.config import JsonDict
5157
from pydantic.json_schema import GenerateJsonSchema
5258
else:
5359
from pydantic.class_validators import root_validator
@@ -216,6 +222,30 @@ def model_json_schema(cls, *args, **kwargs) -> Dict[str, Any]:
216222
kwargs.setdefault("schema_generator", CustomGenerateJsonSchema)
217223
return super().model_json_schema(*args, **kwargs)
218224

225+
# Pydantic does not initialize either `__fields__` or `model_fields` in the __init_subclass__
226+
# method anymore in V2, thus we have to call this logic here as well
227+
@classmethod
228+
def __pydantic_init_subclass__(cls, **kwargs: Any) -> None:
229+
super().__pydantic_init_subclass__(**kwargs)
230+
231+
for _, field in get_model_fields(cls).items():
232+
point_index = getattr(get_field_type(field), "_point_index", False)
233+
range_index = getattr(get_field_type(field), "_range_index", False)
234+
text_index = getattr(get_field_type(field), "_text_index", False)
235+
unique = getattr(get_field_type(field), "_unique", False)
236+
237+
if field.json_schema_extra is None:
238+
field.json_schema_extra = {}
239+
240+
if point_index:
241+
cast(JsonDict, field.json_schema_extra)["point_index"] = True
242+
if range_index:
243+
cast(JsonDict, field.json_schema_extra)["range_index"] = True
244+
if text_index:
245+
cast(JsonDict, field.json_schema_extra)["text_index"] = True
246+
if unique:
247+
cast(JsonDict, field.json_schema_extra)["uniqueness_constraint"] = True
248+
219249
else:
220250

221251
@root_validator
@@ -356,6 +386,22 @@ def __init_subclass__(cls, *args, **kwargs) -> None:
356386
else:
357387
setattr(cls._settings, setting, value)
358388

389+
if not IS_PYDANTIC_V2:
390+
for _, field in get_model_fields(cls).items():
391+
point_index = getattr(get_field_type(field), "_point_index", False)
392+
range_index = getattr(get_field_type(field), "_range_index", False)
393+
text_index = getattr(get_field_type(field), "_text_index", False)
394+
unique = getattr(get_field_type(field), "_unique", False)
395+
396+
if point_index:
397+
field.field_info.extra["point_index"] = True
398+
if range_index:
399+
field.field_info.extra["range_index"] = True
400+
if text_index:
401+
field.field_info.extra["text_index"] = True
402+
if unique:
403+
field.field_info.extra["uniqueness_constraint"] = True
404+
359405
super().__init_subclass__(*args, **kwargs)
360406

361407
def __eq__(self, other: Any) -> bool:

pyneo4j_ogm/fields/property_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ def __new__(cls, *args, **kwargs):
4949

5050
@classmethod
5151
def __get_pydantic_core_schema__(cls, _: Any, handler: GetCoreSchemaHandler) -> CoreSchema: # type: ignore
52-
return handler(property_type) # type: ignore
52+
return handler(property_type)
5353

5454
return PropertyWithOptions

0 commit comments

Comments
 (0)