简体   繁体   English

InternalError:当前事务被中止,命令被忽略,直到事务块结束(受UNIQUE约束)

[英]InternalError: current transaction is aborted, commands ignored until end of transaction block ,on UNIQUE constraint

I tried to get executed with my except: statement... while attempt to oppose the functionality of UNIQUE constraint..But ended with exceptional error.. The Postgresql database table already contains the row that I have used in 我试图用我的except:语句执行...,同时试图反对UNIQUE约束的功能。但是以异常错误结束。.Postgresql数据库表已经包含我在其中使用的行

db.insert("The News","AparnaKumar",1995,234569654) db.insert(“新闻”,“ AparnaKumar”,1995,234569654)

but it works well on inserting unrepeated rows.. 但在插入未重复的行时效果很好。

import psycopg2
class database:

    def __init__(self):

        self.con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432' ")
        self.cur=self.con.cursor()
        self.cur.execute("CREATE TABLE if not exists books(id SERIAL PRIMARY KEY,title TEXT NOT NULL UNIQUE,author TEXT NOT NULL,year integer NOT NULL,isbn integer NOT NULL UNIQUE)")
        self.con.commit()

    def insert(self,title,author,year,isbn):
      try:
        self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
        self.con.commit()
      except:
          print("already exists..")

    def view(self):
        self.cur.execute("SELECT * FROM books")
        rows=self.cur.fetchall()
        print(rows)

    def search(self,title=None,author=None,year=None,isbn=None):
        self.cur.execute("SELECT * FROM books WHERE title=%s or author=%s or year=%s or isbn=%s",(title,author,year,isbn))
        row=self.cur.fetchall()
        print(row)

db=database()
db.insert("The News","AparnaKumar",1995,234569654)
db.view()
db.search(year=1995)

InternalError:当前事务中止,命令被忽略,直到事务块结束

You can either modify your python function to rollback the transaction, or modify the sql to not insert a new row if a conflict occurs (postgresql versions 9.5+): 您可以修改python函数以回滚事务,或者修改sql以在发生冲突时不插入新行(PostgreSQL版本9.5+):

option 1: 选项1:

def insert(self,title,author,year,isbn):
  try:
    self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
    self.con.commit()
  except:
    self.con.rollback()
      print("already exists..")

option 2 (works for postgresql versions 9.5 or later): 选项2(适用于PostgreSQL 9.5或更高版本):

def insert(self,title,author,year,isbn):
  try:
    self.cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s) ON CONFLICT DO NOTHING",(title,author,year,isbn))
    self.con.commit()
  except:
      print("already exists..")

Use a SAVEPOINT . 使用SAVEPOINT

See the second question in the psycopg FAQ list . 请参阅psycopg常见问题解答列表中的第二个问题。

If you want to keep an transaction open, you should use a SAVEPOINT and ROLLBACK to that SAVEPOINT when the exception occurs. 如果要保持事务打开,则应在发生异常时使用SAVEPOINT和ROLLBACK到该SAVEPOINT。 But since you use a catchall exception (too broad), this will also happen on other errors. 但是,由于使用了包罗万象的异常(范围太广),因此在其他错误上也会发生这种情况。 So it gets a bit out of control. 因此它有点失控。

Perhaps a very simple solution is to check using a SELECT if the record you're about to insert already exists. 也许非常简单的解决方案是使用SELECT检查要插入的记录是否已经存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 psycopg2.errors.InFailedSqlTransaction:当前事务被中止,命令被忽略直到事务块结束 - psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block Django 原子事务句柄唯一约束 - Django atomic transaction handle unique constraint Django刷新数据库直到事务结束 - Django Refresh database until end of transaction SQLAlchemy:在回滚无效事务之前无法重新连接 - SQLAlchemy: Can't reconnect until invalid transaction is rolled back aiohttp + sqlalchemy:在回滚无效事务之前无法重新连接 - aiohttp+sqlalchemy: Can't reconnect until invalid transaction is rolled back 尝试创建 postgresql 数据库时,Django 抛出“[12728] 错误:CREATE DATABASE 无法在事务块内运行” - Django throws “[12728] ERROR: CREATE DATABASE cannot run inside a transaction block” when tried to create postgresql database discord.ext.commands.errors.CommandInvokeError:命令引发异常:IntegrityError:唯一约束失败:main.Players - discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IntegrityError: UNIQUE constraint failed: main.Players SendRawTransaction 返回 hash,但是没有事务 - SendRawTransaction return hash, but there is no transaction 等待内存池中的交易 - Awaiting transaction in the mempool 由于会话事务集合丢失,因此无法保持事务状态 - Unable to persist transaction state because the session transaction collection is missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM