title: How to use Factory Boy with SQLAlchemy session (Lazy Loaded) correctly?
body: <p>I just run into a problem with my testsuite.
  I am using a setup with nosetests, SQLAlchemy, Flask and Factory-Boy</p>
  
  <p>I have the following code:</p>
  
  <pre><code>def _create_fixtures(self):
      self.user = UserFactory()
      pprint(db.query(User).all())
      db.add(self.user)
      pprint(db.query(User).all())
  </code></pre>
  
  <p>witch returns following:</p>
  
  <pre><code>[&lt;User #1&gt;, &lt;User #2&gt;]
  [&lt;User #1&gt;, &lt;User #2&gt;]
  </code></pre>
  
  <p>My UserFactory looks like this:</p>
  
  <pre><code>class UserFactory(Factory):
      FACTORY_FOR = User
      FACTORY_SESSION = db
      email = Sequence(lambda n: 'user{0}@wohnortfinder.com'.format(n))
      password = "password"
      username = Sequence(lambda n: 'user{0}'.format(n))
  </code></pre>
  
  <p>(yes I am using the normal Factory and not he SQLAlchemy factory, cause this didn't work either)</p>
  
  <p>Why isn't my Factory Object stored to db? It doesn't raise an error, it just doesn't save.
  I mean even when the current transaction is not committed yet, the query later should queries the actual transaction, shouldn't it?</p>
  
  <p>Oddly when I manually commit the session, it raises an error.</p>
  
  <pre><code>InvalidRequestError: No transaction is begun.
  </code></pre>
  
  <p>Even though I began a transaction when creating the session object.</p>
  
  <pre><code>def init_engine(uri, **kwargs):
      global engine
      engine = create_engine(uri, **kwargs)
      Base.metadata.create_all(engine)
      db.begin()
      return engine
  
  
  engine = None
  
  db = scoped_session(lambda: create_session(bind=engine))
  </code></pre>
  
  <p>any idea why this is not working?</p>
  
  <p>Thanks for your thoughts</p>