Flask/SQLAlchemy - error creating models with relationships
<p>I created a table <code>users</code> with this mysql statement in the shell:</p>

<pre><code>CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(40) NOT NULL,
  `email` varchar(120) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB;
</code></pre>

<p>and a table <code>posts</code> with this statement:</p>

<pre><code>CREATE TABLE `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `text` varchar(140) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_user_id` (`user_id`),
  CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;
</code></pre>

<p>I want to use these tables in my Flask app. I created a foreign key connecting <code>posts</code> to <code>users</code>. It's supposed to be a one-to-many relationship with the user having many posts.</p>

<p>In my python code I have the following models:</p>

<pre><code>class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(40), unique=True)
    email = db.Column(db.String(120), unique=True)
    posts = db.relationship('Posts', backref='user', lazy='dynamic')

    def __init__(self, username, email):
        self.username = username
        self.email = email

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


class Posts(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String(140))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
</code></pre>

<p>When I try running my Flask app however, I keep getting the error:</p>

<blockquote>
  <p>sqlalchemy.exc.InvalidRequestError: One or more mappers failed to
  initialize - can't proceed with initialization of other mappers.
  Original exception was: Could not determine join condition between
  parent/child tables on relationship Users.posts - there are no foreign
  keys linking these tables.</p>
</blockquote>

<p>But I created the foreign key connecting <code>posts</code> to <code>users</code> in the mysql statement. When I take out the line <code>posts = db.relationship('Posts', backref='user', lazy='dynamic')</code>, everything works just fine, so the issue lies in that piece of code. What part of my setup is wrong?</p>