title: How do I fix sqlalchemy.exc.NoForeignKeysError?
body: <p>I am writing a program that each time city is submitted it should display and let user know it was added. I also got this message when I run the program.
  &quot;sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship TopCities.posts - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.&quot;</p>
  <pre><code>Here is my code:
  
  from app import db
  from datetime import datetime
  
  class TopCities(db.Model):
      #create column for database
      #primary_key means it is unique
      id = db.Column(db.Integer, primary_key=True)
      city_name= db.Column(db.String(64), unique=True, index=True)
      city_rank = db.Column(db.Integer())
      posts = db.relationship('Post', backref='author', lazy='dynamic')
  
      def __repr__(self):
          return f'{self.city_name} with rank {self.city_rank}'
  
  class Post(db.Model):
      #create column for database
      #primary_key means it is unique
      id = db.Column(db.Integer, primary_key=True)
      body = db.Column(db.String(256))
      timestamp = db.Column(db.DateTime, default=datetime.utcnow)
      user_id = db.Column(db.Integer, db.ForeignKey('TopCities.id'))
  
      def __repr__(self):
          return f'&lt;Post&gt; {self.id}: {self.body}&gt;'
  
  
  
  from app import app_obj
  from app.forms import TopCities
  from flask import render_template, flash, redirect
  from app import db
  from app.models import TopCities, Post
  
  @app_obj.route(&quot;/login&quot;, methods = ['GET','POST'])
  def login():
      name = 'Khuong'
      title = 'Top Cities'
      top_cities = TopCities.query.all()
      form = TopCities()
      if form.validate_on_submit():
      top_cities = Topcities.query.all(city_name=form.city_name.data)
          if top_cities is None or not top_cities.check_city_rank(form.city_rank.data):
              flask('Invalid city_name or city_rank')
          else:
              top_cities = TopCities(city_name = form.city_name.data, city_rank = form.city_rank.data)
              db.sesion.add(top_cities)
              db.session.commit()
              flask('New city is added!')
              return redirect('/')
  
      return render_template(&quot;home.html&quot;, name=name, title=title, form=form)
  </code></pre>