Skip to content

Improve get_declaring_class by avoiding iterations #613

@GianlucaFicarelli

Description

@GianlucaFicarelli

Description

In db.utils, improve get_declaring_class by avoiding the iteration over all the registry mappers, to make this function more efficient, since it's called in many endpoints.

Current code:

entitycore/app/db/utils.py

Lines 190 to 204 in 01236ce

def get_declaring_class[I: Identifiable](
db_model_class: type[I], column_name: str
) -> type[I] | None:
"""Return the class that has a table with project id or None."""
if not has_project_id_in_columns(db_model_class):
return None
declaring_table = db_model_class.__mapper__.columns[column_name].table
# Iterate all mappers registered with the Base registry
for mapper in db_model_class.registry.mappers:
if mapper.local_table is declaring_table:
return mapper.class_
return None

Acceptance criteria

  • No iterations

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions