title: Python3 SQLAlchemy sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper or this Session
body: <p>Working with SQL Alchemy for the first time and I keep getting an error when trying to commit.</p>
  
  <p>Python3
  SQLAlchemy 1.0.12</p>
  
  <h3>database.py</h3>
  
  <pre><code>from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  
  engine = create_engine("sqlite:///db.test")
  db_session = scoped_session(sessionmaker(autocommit=False,
                                           autoflush=False,
                                           bind=engine))
  Base = declarative_base()
  Base.query = db_session.query_property()
  
  
  def init_db():
      # import all modules here that might define models so that
      # they will be registered properly on the metadata.  Otherwise
      # you will have to import them first before calling init_db()
      import src.models
      Base.metadata.create_all(bind=engine)
  </code></pre>
  
  <h3>models.py</h3>
  
  <pre><code>from datetime import datetime
  
  from typing import List
  
  from sqlalchemy import Column, Table
  from sqlalchemy import Integer, String, DateTime
  from sqlalchemy import ForeignKey
  from sqlalchemy.orm import relationship
  
  from src.database import Base
  
  class Answer(Base):
      """
      The answer for a question.
      """
      __tablename__ = "answer"
  
      answer_id = Column(Integer(), primary_key=True)
      answer = Column(String(), nullable=False, index=True)
      question_id = Column(Integer(), ForeignKey("question.question_id"))
  
      question = relationship("Question", back_populates="answer")
  
      def __init__(self, answer: str, question_id: int = None):
          self.answer = answer
          self.question_id = question_id
  
  class Question(Base):
      __tablename__ = "question"
  
      question_id = Column(Integer(), primary_key=True)
      question = Column(String(), nullable=False, unique=True, index=True)
      answer = relationship("Answer", uselist=False, back_populates="question")
  
      def __init__(self, question: str, answer: Answer, options: List[Option] = None):
          self.question = question
          self.answer = answer
          if options:
              self.options = options
  </code></pre>
  
  <h3>Create database</h3>
  
  <pre><code>&gt;&gt;&gt; from src.database import init_db
  &gt;&gt;&gt; init_db()
  </code></pre>
  
  <p>This creates the database as expected</p>
  
  <h3>Add items to the database</h3>
  
  <pre><code>&gt;&gt;&gt; from src.models import Question, Answer
  &gt;&gt;&gt; a = Answer("Yes")
  &gt;&gt;&gt; q = Question("Doctor Who?", a)
  &gt;&gt;&gt; from sqlalchemy.orm import Session
  &gt;&gt;&gt; s = Session()
  &gt;&gt;&gt; s.add(a)
  &gt;&gt;&gt; s.add(q)
  </code></pre>
  
  <p>Until now I did not get an error</p>
  
  <pre><code>&gt;&gt;&gt; s.commit()
  </code></pre>
  
  <p>Here I get the error:</p>
  
  <pre><code>sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|Question|question or this Session
  </code></pre>
  
  <p>What can I do to get this working?</p>