0

At the end of an insertion query, twistar calls lastval() and causes the postgres driver to fail.

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? i have no control how twistar calling lastval

here's the code who caused that

def __user_done(self, user):
    if len(user.errors) > 0:
        print '%s errors in user creation' % len(user.errors)
        print user.errors
    else:
        logging.debug("My user created. uuid is %s and username is %s" % (user.user_id, user.username))

def insert_my_user(self, name):
    """Inserisce il proprio utente con nome dato e uuid randomico"""
    extras.register_uuid()
    my_uuid = uuid4()
    extensions.adapt(my_uuid).getquoted()
    me = My_user(user_id=my_uuid, username=name)
    me.save().addCallback(self.__user_done)
piro
  • 13,378
  • 5
  • 34
  • 38
venturieffect
  • 181
  • 2
  • 10

1 Answers1

0

If somebody else has the same problem here's how the developer solved to me:

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.

venturieffect
  • 181
  • 2
  • 10