title: How to set the default value of a column in sqlalchemy to the value of a column from a relationship?
body: <p>I am setting up a Sqlalchemy mapper for a sqlite database. My User class has a non-nullable relationship with my Team class. The code I already have is as follows:</p>
  
  <pre><code>class Team(Base):
      __tablename__ = 'teams'
  
      team_id = Column(Integer, primary_key=True)
      # Using Integer as holder for boolean
      is_local = Column(Integer, default=0)
  
  class User(Base):
      __tablename__ = 'users'
  
      user_id = Column(Integer, primary_key=True)
      team_id = Column(Integer, ForeignKey(Team.team_id), default=1, nullable=False)
      team = relationship('Team')
      is_local = Column(Integer, default=0)
  </code></pre>
  
  <p>I would like to establish that the value of <code>User.is_local</code> is by default the value of <code>Team.is_local</code> for the User's linked team. 
  <em>However</em>, after the creation of the User, I would still like the ability to modify the user's <code>is_local</code> value without changing the values of the team or any other user on the team.</p>
  
  <p>So if I were to execute</p>
  
  <pre><code>faraway = Team(is_local=1)
  session.add(faraway)
  session.commit()
  
  u = User(team=faraway)
  session.add(u)
  session.commit()
  
  print(bool(u.is_local))
  </code></pre>
  
  <p>The result should be <code>True</code></p>
  
  <p>So far, I have tried context-sensitive default functions as suggested by <a href="https://stackoverflow.com/a/36579924">https://stackoverflow.com/a/36579924</a>, but I have not been able to find the syntax allowing me to reference <code>Team.is_local</code></p>
  
  <p>Is there a simple way to do this?</p>