Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Postgres backend Record is a Mapping but some Mapping methods are deprecated #407

Description

@ugtar

Since #299 upgraded to sqlalchemy 1.4, the postgres backend's Record object now mimics the behavior of sqlalchemy's Row which is meant to behave similarly to a NamedTuple (and inherits from collections.abc.Sequence) https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#change-4710-core

Meanwhile, postgres backend's Record object inherits from collections.abc.Mapping and is therefore required to fulfill the Mapping interface, which includes keys() and values() which are now deprecated.

Sqlalchemy provides a mapping() method on Result which will cause it to return RowMapping objects rather than Row objects, and those look like Mappings.

I encountered this issue working with fastapi and pydantic. Returning Records as pydantic models worked in the past, but now produces a deprecation warning (and i guess will eventually stop working) since pydantic's builtin validator treats the Record as a Mapping and attempts to call dict(record)

Activity

  1. ugtar commented on Oct 11, 2021

    @ugtar
    Author

    the workaround I suppose is to do something like

    results = await database.fetch_all(query)
    return [r._mapping() for r in results]

    instead of simply returning results as before. Still, I hope that having a correct type for Record will make that unnecessary.

  2. ugtar commented on Oct 12, 2021

    @ugtar
    Author

    FYI I did some more testing, and I think that changing the parent class of Record from collections.abc.Mapping to collections.abc.Sequence does not solve the particular issue with pydantic attempting to dict the Record.

  3. aminalaee commented on Oct 12, 2021

    @aminalaee
    Contributor

    Sorry I'm a little confused now, I don't know much about Pedantic.
    Do you mean that this works in Pydantic?

    [r._mapping for r in results]

    But calling dict(record) produces DeprecationWarning?

  4. ugtar commented on Oct 12, 2021

    @ugtar
    Author
  5. aminalaee commented on Oct 12, 2021

    @aminalaee
    Contributor

    @ugtar Yes in that case you are right, Mapping should implement keys() and values(), items(), etc but it doesn't.
    I don't see the point in keeping that a Mapping anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clean upRefinement to existing functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions