Flask-SQLAlchemy - Backref isn't working, value is None
<p>I think I have the same issue as <a href="http://stackoverflow.com/questions/28323644/flask-sqlalchemy-backref-not-working">here on SO.</a> Using python 3.5, flask-sqlalchemy, and sqlite. I am trying to establish a one (User) to many (Post) relationship.</p>

<pre><code>class User(db_blog.Model):
    id = db_blog.Column(db_blog.Integer, primary_key=True)
    nickname = db_blog.Column(db_blog.String(64), index=True, unique=True)
    email = db_blog.Column(db_blog.String(120), unique=True)
    posts = db_blog.relationship('Post', backref='author', lazy='dynamic')

    def __repr__(self):
        return '&lt;User: {0}&gt;'.format(self.nickname)

class Post(db_blog.Model):
    id = db_blog.Column(db_blog.Integer, primary_key=True)
    body = db_blog.Column(db_blog.String(2500))
    title = db_blog.Column(db_blog.String(140))
    user_id = db_blog.Column(db_blog.Integer, db_blog.ForeignKey('user.id'))

    def __init__(self, body, title, **kwargs):
        self.body = body
        self.title = title

    def __repr__(self):
        return '&lt;Title: {0}\nPost: {1}\nAuthor: {2}&gt;'.format(self.title, self.body, self.author)
</code></pre>

<p>Author is None.</p>

<pre><code>&gt;&gt;&gt; u = User(nickname="John Doe", email="jdoe@email.com")
&gt;&gt;&gt; u
&lt;User: John Doe&gt;
&gt;&gt;&gt; db_blog.session.add(u)
&gt;&gt;&gt; db_blog.session.commit()
&gt;&gt;&gt; u = User.query.get(1)
&gt;&gt;&gt; u
&lt;User: John Doe&gt;
&gt;&gt;&gt; p = Post(body="Body of post", title="Title of Post", author=u)
&gt;&gt;&gt; p
&lt;Title: Title of Post
Post: Body of post
Author: None&gt; #Here I expect- "Author: John Doe&gt;"
</code></pre>

<p>I get the same result after session add/commit of the post and so can't find the post author.</p>

<p>You added your own <code>__init__</code> to <code>Post</code>. While it accepts keyword arguments, it does nothing with them. You can either update it to use them</p>

<pre><code>def __init__(self, body, title, **kwargs):
    self.body = body 
    self.title = title
    for k, v in kwargs:
        setattr(self, k, v)
</code></pre>

<p>Or, ideally, you can just remove the method and let SQLAlchemy handle it for you. </p>

python sqlite flask sqlalchemy flask-sqlalchemy