bmuller / twistar

Twistar is an object-relational mapper (ORM) for Python that uses the Twisted library to provide asynchronous DB interaction.
http://findingscience.com/twistar
Other
132 stars 38 forks source link

Twistar calling lastval() causing error in psycopg2 #71

Closed aaleotti-unimore closed 7 years ago

aaleotti-unimore commented 7 years ago

At the end onf an insertion query, twistar calls lastval() and causes the postgres driver to fail. how to solve that?

2016-10-06` 11:08:02+0200 [-] Log opened.
2016-10-06 11:08:02+0200 [-] MAIN: Starting the reactor
2016-10-06 11:08:02+0200 [-] TWISTAR query: SELECT * FROM my_user WHERE user_id = %s LIMIT 1
2016-10-06 11:08:02+0200 [-] TWISTAR args: 009a65e7-a6a8-4de4-ad1a-87ac20e4073e
2016-10-06 11:08:02+0200 [-] TWISTAR query: SELECT * FROM my_user LIMIT 1
2016-10-06 11:08:02+0200 [-] TWISTAR query: INSERT INTO my_user ("username","user_id") VALUES (%s,%s)
2016-10-06 11:08:02+0200 [-] TWISTAR args: myusername,009a65e7-a6a8-4de4-ad1a-87ac20e4073e
2016-10-06 11:08:02+0200 [-] TWISTAR query: SELECT lastval()
2016-10-06 11:08:02+0200 [-] Unhandled error in Deferred:
2016-10-06 11:08:02+0200 [-] Unhandled Error
Traceback (most recent call last):
      File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
        self.run()
      File "/usr/lib/python2.7/threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "/usr/lib/python2.7/site-packages/twisted/_threads/_threadworker.py", line 46, in work
        task()
      File "/usr/lib/python2.7/site-packages/twisted/_threads/_team.py", line 190, in doWork
        task()
    --- <exception caught here> ---
      File "/usr/lib/python2.7/site-packages/twisted/python/threadpool.py", line 246, in inContext
        result = inContext.theWork()
      File "/usr/lib/python2.7/site-packages/twisted/python/threadpool.py", line 262, in <lambda>
        inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
      File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
        return func(*args,**kw)
      File "/usr/lib/python2.7/site-packages/twisted/enterprise/adbapi.py", line 477, in _runInteraction
        compat.reraise(excValue, excTraceback)
      File "/usr/lib/python2.7/site-packages/twisted/enterprise/adbapi.py", line 467, in _runInteraction
        result = interaction(trans, *args, **kw)
      File "/usr/lib/python2.7/site-packages/twistar/dbconfig/base.py", line 348, in _doinsert
        self.insert(tablename, vals, txn)
      File "/usr/lib/python2.7/site-packages/twistar/dbconfig/base.py", line 192, in insert
        return self.getLastInsertID(txn)
      File "/usr/lib/python2.7/site-packages/twistar/dbconfig/postgres.py", line 9, in getLastInsertID
        self.executeTxn(txn, q)
      File "/usr/lib/python2.7/site-packages/twistar/dbconfig/base.py", line 78, in executeTxn
        return txn.execute(query, *args, **kwargs)
    psycopg2.OperationalError: ERRORE:  lastval non è stato ancora definito in questa sessione

last line says "lastval not yet defined in this session"

how to avoid that?

bmuller commented 7 years ago

I think the issue is that you're explicitly setting the id column. Twistar is designed to use autoincrementing id values at the DB level (in the case of Postgres, this would be a SERIAL PRIMARY KEY column type), which is why you don't have a lastval defined.

aaleotti-unimore commented 7 years ago

thanks that solved me everything.

I suggest to specify that more explicitly in the docs PS: thanks for all the work. twistar solves me a lot of tedious SQL scripting