Base = declarative_base()
+.. _relationship_patterns_o2m:
+
One To Many
~~~~~~~~~~~
:ref:`cascade_delete_orphan`
+.. _relationship_patterns_m2o:
+
Many To One
~~~~~~~~~~~
~~~~~~~~~~
One To One is essentially a bidirectional relationship with a scalar
-attribute on both sides. To achieve this, the :paramref:`_orm.relationship.uselist` flag indicates
+attribute on both sides. To achieve this, the :paramref:`_orm.relationship.uselist` flag
+set to a value of ``False`` indicates
the placement of a scalar attribute instead of a collection on the "many" side
-of the relationship. To convert one-to-many into one-to-one::
+of the relationship.
+
+For example, to convert a :ref:`one-to-many <relationship_patterns_o2m>`
+relationship into one-to-one, we add ``uselist=False`` to what would normally
+be the "many" side of the relationship, renaming ``Parent.children`` to
+``Parent.child`` for clarity::
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
- child = relationship("Child", uselist=False, back_populates="parent")
+
+ # this was previously Parent.children
+ child = relationship("Child", back_populates="parent", uselist=False)
class Child(Base):
__tablename__ = 'child'
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", back_populates="child")
-Or for many-to-one::
+Similarly, to convert a :ref:`many-to-one <relationship_patterns_m2o>`
+relationship into one-to-one, we again apply ``uselist=False`` to the
+"many" side, in the below example renaming ``Child.parents`` to ``Child.parent``::
class Parent(Base):
__tablename__ = 'parent'
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
+
+ # this was previously Child.parents
parent = relationship("Parent", back_populates="child", uselist=False)
As always, the :paramref:`_orm.relationship.backref` and :func:`.backref` functions