--- /dev/null
+.. change::
+ :tags: bug, orm
+ :tickets: 10815
+
+ Fixed issue where ORM Annotated Declarative would mis-interpret the left
+ hand side of a relationship without any collection specified as
+ uselist=True if the left type were given as a class and not a string,
+ without using future-style annotations.
argument, originating_module
)
- # we don't allow the collection class to be a
- # __forward_arg__ right now, so if we see a forward arg here,
- # we know there was no collection class either
- if (
- self.collection_class is None
- and not is_write_only
- and not is_dynamic
- ):
- self.uselist = False
+ if (
+ self.collection_class is None
+ and not is_write_only
+ and not is_dynamic
+ ):
+ self.uselist = False
# ticket #8759
# if a lead argument was given to relationship(), like
is_false(B.__mapper__.attrs["a"].uselist)
is_false(B.__mapper__.attrs["a_warg"].uselist)
- def test_one_to_one_example(self, decl_base: Type[DeclarativeBase]):
+ def test_one_to_one_example_quoted(self, decl_base: Type[DeclarativeBase]):
"""test example in the relationship docs will derive uselist=False
correctly"""
is_(p1.child, c1)
is_(c1.parent, p1)
+ def test_one_to_one_example_non_quoted(
+ self, decl_base: Type[DeclarativeBase]
+ ):
+ """test example in the relationship docs will derive uselist=False
+ correctly"""
+
+ class Child(decl_base):
+ __tablename__ = "child"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent.id"))
+ parent: Mapped["Parent"] = relationship(back_populates="child")
+
+ class Parent(decl_base):
+ __tablename__ = "parent"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ child: Mapped[Child] = relationship( # noqa: F821
+ back_populates="parent"
+ )
+
+ c1 = Child()
+ p1 = Parent(child=c1)
+ is_(p1.child, c1)
+ is_(c1.parent, p1)
+
def test_collection_class_dict_no_collection(self, decl_base):
class A(decl_base):
__tablename__ = "a"
is_false(B.__mapper__.attrs["a"].uselist)
is_false(B.__mapper__.attrs["a_warg"].uselist)
- def test_one_to_one_example(self, decl_base: Type[DeclarativeBase]):
+ def test_one_to_one_example_quoted(self, decl_base: Type[DeclarativeBase]):
"""test example in the relationship docs will derive uselist=False
correctly"""
is_(p1.child, c1)
is_(c1.parent, p1)
+ def test_one_to_one_example_non_quoted(
+ self, decl_base: Type[DeclarativeBase]
+ ):
+ """test example in the relationship docs will derive uselist=False
+ correctly"""
+
+ class Child(decl_base):
+ __tablename__ = "child"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent.id"))
+ parent: Mapped["Parent"] = relationship(back_populates="child")
+
+ class Parent(decl_base):
+ __tablename__ = "parent"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ child: Mapped[Child] = relationship( # noqa: F821
+ back_populates="parent"
+ )
+
+ c1 = Child()
+ p1 = Parent(child=c1)
+ is_(p1.child, c1)
+ is_(c1.parent, p1)
+
def test_collection_class_dict_no_collection(self, decl_base):
class A(decl_base):
__tablename__ = "a"