Discussed in #9368
from typing import List
from sqlalchemy import Column, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.types import Integer
from sqlalchemy.orm import declarative_base
# qualify the base with __allow_unmapped__. Can also be
# applied to classes directly if preferred
class Base:
__allow_unmapped__ = True
# raises, but shouldn't (right?)
id: int = Column(Integer, primary_key=True)
Base = declarative_base(cls=Base)
# existing mapping proceeds, Declarative will ignore any annotations
# which don't include ``Mapped[]``
class Foo(Base):
__tablename__ = "foo"
bars: List["Bar"] = relationship("Bar", back_populates="foo")
class Bar(Base):
__tablename__ = "bar"
foo_id = Column(ForeignKey("foo.id"))
foo: Foo = relationship(Foo, back_populates="bars", cascade="all")
Discussed in #9368