title: Self-referencing a table for a foreign key relationship in Flask-Sqlalchemy
body: <p>I'm sure there is an easy solution to this problem but I haven't been able to find it or think it. I have a model &quot;Entry&quot; which are supposed to be in sequence with each other (For instance entry 1 might be the first part of a blog post, entry 2 the second part and so on)</p>
  <pre><code>class Entry(db.Model):
          __tablename__=&quot;entry&quot;
          id = db.Column(db.Integer, primary_key=True)
                  ...
  
          prev_entry_id = db.Column(db.Integer, db.ForeignKey('entry.id'))
          next_entry_id = db.Column(db.Integer, db.ForeignKey('entry.id'))
          prev_entry = db.relationship(&quot;Entry&quot;, backref='next', remote_side='entry.id', foreign_keys=[prev_entry_id])  #previous_entry object
          next_entry = db.relationship(&quot;Entry&quot;,  backref=&quot;previous&quot;, uselist=False, foreign_keys=[next_entry_id])  #next_entry object
  </code></pre>
  <p>The idea is if you get one entry, say entry-56 you can navigate forward (57) and backward (55), step-by-step, all the way through, as if it were a pointer.</p>
  <p>However I'm getting a ton of errors from sqlalchemy:</p>
  <pre><code>Table has no attribute 'id'
  </code></pre>
  <p>If I comment out &quot;prev_entry&quot; I get:</p>
  <pre><code>sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Entry-&gt;entry'. Original exception was: 'Table' object has no attribute 'id'
  </code></pre>
  <p>If I comment out both &quot;next_*entry&quot; I get: <code>Table has no attribute 'id'</code></p>
  <p>If I comment out both &quot;next_entry&quot;* and &quot;prev_entry&quot; it at least loads.</p>
  <p>In short, does anyone know the correct way to set this up in Flask-SqlAlchemy?</p>