Twisted - Twistar 调用 lastval() 导致 psycopg2 错误

Twisted - Twistar calling lastval() causing psycopg2 error

在插入查询结束时,twistar 调用 lastval() 并导致 postgres 驱动程序失败。

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

最后一行说 "lastval not yet defined in this session"

如何避免这种情况?我无法控制 twistar 如何调用 lastval

这是导致该问题的代码

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)

如果其他人遇到同样的问题,开发人员是这样解决我的:

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.