title: SQLAlchemy: __init__() takes 1 positional argument but 2 were given (many to many)
body: <p>SQLAlchemy is undoubtedly very powerful, but the documentation implicitly assumes lots of prior knowledge and on the subject of relationships, mixes between the <code>backref</code> and the newly-preferred <code>back_populates()</code> methods, which I find very confusing.</p>
  
  <p>The following model design is pretty much an exact mirror of the guide in the documentation that deals with the <a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects" rel="noreferrer">Association Objects for many-to-many relationships</a>. You can see that the comments are still identical to those in the original article, and I've only changed the actual code.</p>
  
  <pre><code>class MatchTeams(db.Model):
      match_id = db.Column(db.String, db.ForeignKey('match.id'), primary_key=True)
      team_id = db.Column(db.String, db.ForeignKey('team.id'), primary_key=True)
      team_score = db.Column(db.Integer, nullable="True")
  
      # bidirectional attribute/collection of "user"/"user_keywords"
      match = db.relationship("Match",
                              backref=db.backref("match_teams",
                                              cascade="all, delete-orphan")
                              )
      # reference to the "Keyword" object
      team = db.relationship("Team")
  
  
  class Match(db.Model):
      id = db.Column(db.String, primary_key=True)
  
      # Many side of many to one with Round
      round_id = db.Column(db.Integer, ForeignKey('round.id'))
      round = db.relationship("Round", back_populates="matches")
      # Start of M2M
  
      # association proxy of "match_teams" collection
      # to "team" attribute
      teams = association_proxy('match_teams', 'team')
  
      def __repr__(self):
          return '&lt;Match: %r&gt;' % (self.id)
  
  
  class Team(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String, nullable=False)
      goals_for = db.Column(db.Integer)
      goals_against = db.Column(db.Integer)
      wins = db.Column(db.Integer)
      losses = db.Column(db.Integer)
      points = db.Column(db.Integer)
      matches_played = db.Column(db.Integer)
  
      def __repr__(self):
          return '&lt;Team %r with ID: %r&gt;' % (self.name, self.id)
  </code></pre>
  
  <p>But this snippet, which is supposed to associate the team instance <code>find_liverpool</code> with the match instance <code>find_match</code> (both boilerplate objects), doesn't work:</p>
  
  <pre><code>find_liverpool = Team.query.filter(Team.id==1).first()
  print(find_liverpool)
  find_match = Match.query.filter(Match.id=="123").first()
  print(find_match)
  
  find_match.teams.append(find_liverpool)
  </code></pre>
  
  <p>And outputs the following:</p>
  
  <pre><code>Traceback (most recent call last):
  File "/REDACT/temp.py", line 12, in &lt;module&gt;
  find_match.teams.append(find_liverpool)
  File "/REDACT/lib/python3.4/site-packages/sqlalchemy/ext/associationproxy.py", line 609, in append
  item = self._create(value)
  File "/REDACT/lib/python3.4/site-packages/sqlalchemy/ext/associationproxy.py", line 532, in _create
   return self.creator(value)
  TypeError: __init__() takes 1 positional argument but 2 were given    
  &lt;Team 'Liverpool' with ID: 1&gt;
  &lt;Match: '123'&gt;
  </code></pre>