Skip to content

mapper is deprecated on SQLAlchemy 1.4+ #41

Open
@staticdev

Description

@staticdev

registry.map_imperatively() should be used instead.

Reference: https://docs.sqlalchemy.org/en/14/orm/mapping_api.html?highlight=mapper#sqlalchemy.orm.mapper

This involves some refactoring and updates on almost all chapters. Not sure what is the path (update the book first or the branches). I am available to help.

What needs to be done:

  1. Instead of importing MetaData on orm.py, you import his parent (registry):
from sqlalchemy.orm import registry

mapper_registry = registry()
  1. On each Table, you pass as second argument mapper_registry.metadata instead of metadata. Eg.:
order_lines = Table(
    "order_lines",
    mapper_registry.metadata,
    Column("id", Integer, primary_key=True, autoincrement=True),
    Column("sku", String(255)),
    Column("qty", Integer, nullable=False),
    Column("orderid", String(255)),
)
  1. You add the classes and tables with mapper_registry.map_imperatively instead of mapper. Eg.:
def start_mappers():
    mapper_registry.map_imperatively(model.OrderLine, order_lines)
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions