#11536·sqlalchemy

support module-qualified paths inside of Mapped[], including with/without future mode / pep563 etc

Author: acarapetisCreated Jun 26, 2024Updated Sep 22, 2026
Labelsormuse caseorm - annotated declarative

Describe the bug

When using PEP563 postponed annotations (i.e. from __future__ import annotations), mapped classes with the same name in different modules cannot be used in relationships using type annotations alone. For example, if mod1 and mod2 both contain mapped classes called Model, then attr: Mapped[mod1.Model] = relationship() produces an error of the form

sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Model" in the registry of this declarative base. Please use a fully module-qualified path.

When mod1.Model really is a fully module-qualified path, this is quite a confusing message!

It would be fantastic if the declarative mapping logic could be adjusted to handle stringified annotations better in this case; but if that's a big ask, a more informative error message telling me I need to use attr: Mapped[mod1.Model] = relationship("mod1.Model") would have saved me some time :)

SQLAlchemy Version in Use

2.0.31

Python Version

3.12

Operating system

Linux

To Reproduce

Requires multiple files to reproduce - I've pushed as a repo here: https://github.com/acarapetis/sa2-relationship-annotation-example

python
### sqla_example/__main__.py
from sqlalchemy import orm
from .model import computers, person, stationery
orm.configure_mappers()

### sqla_example/model/base.py
class Base(DeclarativeBase):
    pass

### sqla_example/model/computers.py
from sqla_example.model.base import Base

class Notebook(Base):
    __tablename__ = "computer_notebook"
    id: Mapped[int] = mapped_column(primary_key=True)

### sqla_example/model/person.py
from __future__ import annotations # Removing this fixes the error

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from sqla_example.model.base import Base
import sqla_example.model.computers

class Person(Base):
    id: Mapped[int] = mapped_column(primary_key=True)
    laptop_id: Mapped[int] = mapped_column(ForeignKey(sqla_example.model.computers.Notebook.id))
    laptop: Mapped["sqla_example.model.computers.Notebook"] = relationship()

### sqla_example/model/stationery.py
from sqlalchemy.orm import Mapped, mapped_column
from sqla_example.model.base import Base

class Notebook(Base):
    __tablename__ = "stationery_notebook"
    id: Mapped[int] = mapped_column(primary_key=True)

Error

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/anthony/github/sa2-relationship-annotation-example/sqla_example/__main__.py", line 5, in <module>
    orm.configure_mappers()
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 4173, in configure_mappers
    _configure_registries(_all_registries(), cascade=True)
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 4204, in _configure_registries
    _do_configure_registries(registries, cascade)
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 4245, in _do_configure_registries
    mapper._post_configure_properties()
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 2405, in _post_configure_properties
    prop.init()
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/interfaces.py", line 584, in init
    self.do_init()
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/relationships.py", line 1642, in do_init
    self._setup_entity()
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/relationships.py", line 1854, in _setup_entity
    self._clsregistry_resolve_name(argument)(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.py", line 515, in _resolve_name
    rval = d[token]
           ~^^^^^^^
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/util/_collections.py", line 345, in __missing__
    self[key] = val = self.creator(key)
                      ^^^^^^^^^^^^^^^^^
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.py", line 462, in _access_cls
    return _determine_container(key, decl_class_registry[key])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.py", line 408, in _determine_container
    value = value.attempt_get([], key)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/anthony/github/sa2-relationship-annotation-example/.env/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.py", line 219, in attempt_get
    raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Multiple classes found for path "Notebook" in the registry of this declarative base. Please use a fully module-qualified path.

Additional context

No response