title: sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'title.id' on table 'post'
body: <p>I getting this error:</p>
  
  <blockquote>
    <p>sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'title.id' on table 'post': table 'title' has no column named 'id'</p>
  </blockquote>
  
  <p>when I remove these two line I'm not getting any error:</p>
  
  <p><code>head_id = Column(Integer, ForeignKey('title.id'))</code></p>
  
  <p><code>head = relationship('Title', backref='post')</code></p>
  
  <p>What I'm doing wrong with <code>relationship</code> and <code>foreignKey</code>?</p>
  
  <p><strong>My code <code>main.py</code></strong></p>
  
  <pre><code>from flask import Flask, render_template, request, redirect, url_for
  import datetime
  from sqlalchemy.orm import sessionmaker
  from sqlalchemy import Column, String, Integer, create_engine, ForeignKey
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import relationship
  
  
  # flask documentation &gt;&gt; https://flask.palletsprojects.com/en/1.1.x/quickstart/
  app = Flask(__name__, template_folder='template', static_folder='static')
  
  Base = declarative_base()
  
  date = datetime.datetime.strftime(datetime.datetime.now(), '%d-%b-%Y')
  
  engine = create_engine('mysql://root:@localhost/flask')
  Base.metadata.create_all = engine
  
  database = sessionmaker(bind=engine)
  session = database()
  
  
  
  class contacts(Base):
      __tablename__='contacts'
      id = Column('No.', Integer, primary_key=True)
      name = Column('Name', String(15), nullable=True)
      email = Column('Email', String(20), nullable=True)
      phone = Column('Phone', String(14), nullable=True)
      message = Column('Message', String(100))
      date = Column('Date', String, default=date)
      def __repr__(self):
          return '&lt;contacts(Name=%s, Email=%s, Phone=%s, Message=%s, Date=%s)&gt;' % (
              self.name, self.email, self.phone, self.message, self.date)
  
  
  
  class Title(Base):
      __tablename__='title'
      id = Column('No', Integer, primary_key=True)
      title = Column('Title', String, nullable=False)
      date = Column('Date', String, nullable=False, default=date)
      def __repr__(self):
          return '&lt;head(Title=%s, Date=%s)&gt;' % (self.title, self.date)
  
  class Post(Base):
      __tablename__='post'
      id = Column('No', Integer, primary_key=True)
      post = Column('Post', String, nullable=False)
      head_id = Column(Integer, ForeignKey('title.id'))
      head = relationship('Title', backref='post')
      def __repr__(self):
          return '&lt;post(Post=%s)&gt;' % self.post
  
  @app.route('/')
  def index():
      data = session.query(Post).all()
      return render_template('index.html', data=data)
  
  
  # router for contact ['post' and 'get'] method
  @app.route('/contact', methods=['POST', 'GET'])
  def contact():
      # if 'contact.html' form method is 'post' this will run , otherwise not
      if request.method=='POST':
          # getting value from your 'contact.html' page
          name = request.form['name']
          email = request.form['email']
          phone = request.form['phone']
          message = request.form['message']
  
          # attempt to adding value in database
          try:
              # commit to adding value in database
              value = contacts(name=name, email=email, phone=phone, message=message)
              session.add(value)
              session.commit()
              session.close()
  
              # redirect home page
              return redirect(url_for('index'))
  
          # any problem while adding value in database
          except Exception as e:
              print(e)
      else:
          # not request for 'POST'
          return render_template('contact.html')
  
  # These are all route points (end points)
  @app.route('/post')
  def post():
      return render_template('post.html')
  
  
  @app.route('/about')
  def about():
      return render_template('about.html')
  
  
  
  
  if __name__ == '__main__':
      # run application, and  'debug'= It'll auto configure you changes (you don't need you restart you app again and again'
      app.run(debug=True)
  </code></pre>
  
  <p>How can I create relationship b/w <code>Title</code> and <code>Post</code>?</p>