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

时间:2016-10-06 09:25:41

标签: python postgresql twisted.web

在插入查询结束时,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”

如何避免?我无法控制如何调用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)

1 个答案:

答案 0 :(得分:0)

如果其他人在这里遇到同样的问题,那么开发人员如何解决这个问题:

  

我认为问题在于您明确设置了id列。 Twistar设计用于在DB级别使用自动增量id值(在Postgres的情况下,这将是一个SERIAL PRIMARY KEY列类型),这就是为什么你没有定义lastval。