Skip to content

Duplicate index hashes for pydantic.main are created when models are returned from FastAPI routes #498

Open
@elicore

Description

@elicore

FastAPI creates duplicate models that are used as the returned model from a route. It uses the create_model() function from pydantic.main to do that.

When the duplicate models are being created, they are also queued for migrations in the Migrator class, which creates unnecessary keys in redis for the index hashes in the form of {global_prefix}:pydantic.Main.{class_name}:index:hash.

There are a couple of possible workarounds:

Do not use redis-om-python models as return values from FastAPI routes
This option doesn't make a whole lot of sense and will create lots of duplicate code for extra models instead of reusing the redis-om models (as their pydantic base).

Do not use Migrator().run()
Instead, use this piece of code to filter-out the pydantic hashes:

migrator = aredis_om.Migrator()
await migrator.detect_migrations()
for migration in migrator.migrations:
    if not migration.model_name.lower().startswith('pydantic.main'):
        await migration.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions