redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.07k stars 108 forks source link

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

Open elicore opened 1 year ago

elicore commented 1 year ago

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()