<meta charset="utf-8"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Hi guys,<div><br></div><div>I&#39;ve been going out of my mind the last couple days trying to track down what I&#39;m doing wrong when trying to do fixture tests. I&#39;m building a rest application that is to be used as a billing service. When I got started with fixture (thanks to Ryan McKillen on this mailing list), things were going great. However, I&#39;m starting to get these InvalidRequestErrors, and I&#39;ve kinda hit a brick wall.</div>
<div><br></div><div>The error looks like:</div><div><br></div><div><div>Traceback (most recent call last):</div><div>  File &quot;/var/www/Billing/billing/tests/functional/test_payments.py&quot;, line 16, in setUp</div><div>
    self.data.setup()</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/base.py&quot;, line 71, in setup</div><div>    self.loader.load(self.data)</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/loadable.py&quot;, line 193, in load</div>
<div>    self.wrap_in_transaction(loader, unloading=False)</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/loadable.py&quot;, line 315, in wrap_in_transaction</div><div>
    self.commit()</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/sqlalchemy_loadable.py&quot;, line 149, in commit</div><div>    self.session.flush()</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 1354, in flush</div>
<div>    self._flush(objects)</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 1438, in _flush</div><div>    transaction.rollback()</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 404, in rollback</div>
<div>    transaction._rollback_impl()</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 418, in _rollback_impl</div><div>    self._restore_snapshot()</div>
<div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 294, in _restore_snapshot</div><div>    self.session._update_impl(s)</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 1257, in _update_impl</div>
<div>    self._attach(state)</div><div>  File &quot;/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py&quot;, line 1284, in _attach</div><div>    (mapperutil.state_str(state), state.key)</div>
<div>InvalidRequestError: Can&#39;t attach instance &lt;Product at 0x922478c&gt;; another instance with key (&lt;class &#39;billing.model.product.Product&#39;&gt;, (2,)) is already present in this session.</div></div><div>
<br></div><div>I&#39;ve seen <a href="http://farmdev.com/projects/fixture/using-fixture-with-pylons.html#why-do-i-keep-getting-invalidrequesterror" target="_blank" style="color: rgb(0, 0, 204); ">http://farmdev.com/projects/fixture/using-fixture-with-pylons.html#why-do-i-keep-getting-invalidrequesterror</a> , but I don&#39;t think i&#39;m calling setup() twice before teardown(), unless I&#39;m missing something glaringly obvious. In each test controller, i have this (or something very like this):</div>
<div><br></div><div><div>def setUp(self):</div><div>         super(TestPaymentsController, self).setUp() # in TestController, in tests/__init__.py                                                                              </div>
<div>         self.data = dbfixture.data(AccountData, PaymentData)                                                                                                               </div><div>         self.data.setup()</div><div>
<br></div><div>def tearDown(self):</div><div>         # must truncate table so anything we create gets deleted - fixtures are auto-deleted                                                                               </div>
<div>         meta.Session.execute(&quot;truncate table payment cascade&quot;)                                                                                                             </div><div>         meta.Session.commit()</div>
<div>         self.data.teardown()</div><div>         super(TestPaymentsController, self).tearDown()                                                                                                                     </div>
<div>         meta.Session.remove()</div></div><div><br></div><div>My tests/__init__.py is exactly the same as in the farmdev howto.</div><div><br></div><div>I&#39;ve been throwing the meta.Session.remove() just about anywhere I can, to no avail. I&#39;m sorry to bother you with this, but google doesn&#39;t seem to turn up much on this error. If anyone would be kind enough to help, I&#39;d be very obliged. If you need any more information, please don&#39;t hesitate to ask.</div>
<div><br></div><div>Thanks very much,</div><div>Matt Hoopes</div></span>