sqlalchemy need back_populates?
<p>When I try SQLAlchemy Relation Example following this guide: <a href="http://docs.sqlalchemy.org/en/rel_1_0/orm/basic_relationships.html#one-to-many" rel="nofollow">Basic Relationship Patterns</a></p>

<p>I have this code</p>

<pre><code>#!/usr/bin/env python
# encoding: utf-8
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base(bind=engine)

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent")

Base.metadata.create_all()

p = Parent()
session.add(p)
session.commit()
c = Child(parent_id=p.id)
session.add(c)
session.commit()
print "children: {}".format(p.children[0].id)
print "parent: {}".format(c.parent.id)
</code></pre>

<p>It works well, but in the guide, it says the model should be:</p>

<pre><code>class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    **children = relationship("Child", back_populates="parent")**

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    **parent = relationship("Parent", back_populates="children")**
</code></pre>

<p>Why don't I need <code>back_populates</code> or <code>backref</code> in my example?  When should I use one or the other?</p>

<p>If you use <code>backref</code> you don't need to declare the relationship on the second table.</p>

<pre><code>class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child", backref="parent")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
</code></pre>

<p>If you're <em>not</em> using <code>backref</code>, and defining the <code>relationship</code>'s separately, then if you don't use <code>back_populates</code>, sqlalchemy won't know to connect the relationships, so that modifying one also modifies the other.</p>

<p>So, in your example, where you've defined the <code>relationship</code>'s separately, but didn't provide a <code>back_populates</code> argument, modifying one field wouldn't automatically update the other in your transaction.</p>

<pre><code>&gt;&gt;&gt; parent = Parent()
&gt;&gt;&gt; child = Child()
&gt;&gt;&gt; child.parent = parent
&gt;&gt;&gt; print parent.children
[]
</code></pre>

<p>See how it didn't automatically fill out the <code>children</code> field?  </p>

<p>Now, if you supply a <code>back_populates</code> argument, sqlalchemy will connect the fields.</p>

<pre><code>class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child", back_populates="parent")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", back_populates="children")
</code></pre>

<p>So now we get</p>

<pre><code>&gt;&gt;&gt; parent = Parent()
&gt;&gt;&gt; child = Child()
&gt;&gt;&gt; child.parent = parent
&gt;&gt;&gt; print parent.children
[Child(...)]
</code></pre>

<p>Sqlalchemy knows these two fields are related now, and will update each as the other is updated.  It's worth noting that using <code>backref</code> will do this, too.  Using <code>back_populates</code> is nice if you want to define the relationships on every class, so it's easy to see all the fields just be glancing at the model class, instead of having to look at other classes that define fields via backref.</p>

python sqlalchemy