Skip to content

Flask plugin breaks DeclarativeBase #1361

@pvanderlinden

Description

@pvanderlinden

The plugin breaks the DeclarativeBase, e.g.:

from typing import Any

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.types import JSON

class Base(DeclarativeBase):
    type_annotation_map = {dict[str, Any]: JSON}
db = SQLAlchemy(model_class=Base)

Gives the error:
sqlalchemy.exc.InvalidRequestError: Declarative base class has both a 'registry' attribute and a type_annotation_map entry. Per-base type_annotation_maps are not supported. Please apply the type_annotation_map to this registry directly.

However, type_annotation_map is supported on the base, but by subclassing the base class (something which seems to be happening inside the plugin), it now detects both the registry created from the base class with type_annotation_map and the type_annotation_map. This doesn't happen without the Flask plugin.

Environment:

  • Python version: 3.11.6
  • Flask-SQLAlchemy version: 3.1.1
  • SQLAlchemy version: 2.0.31

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