我有 python 代碼將新記錄推送到 db 到列信息。 try: db.add_new_record(info) except Exception as e: log.error(e) db.db.rollback() continue我使用標(biāo)準(zhǔn)的sql查詢INSERT INTO mydb (info, desc) VALUES ('value1', 'value2')Python:import psycopg2def add_new_record(self, info): info['table'] = self.table query = "INSERT INTO {table} (c1, c2) VALUES ('{val1}', '{val2}')".format(**self.make_dct(info)) self.make_execute(query) self.db_commit() return True有時(shí)我看到異常 exception: duplicate key value violates unique constraint "mydb_info_key"這是正常的。我計(jì)算 db 中的行數(shù):25000 條記錄。但我最后的 ID 以 60000+ 開(kāi)頭。Duplicate exception記錄新ID?如何避免這種情況?
如何避免在數(shù)據(jù)庫(kù)中創(chuàng)建“假”ID?
炎炎設(shè)計(jì)
2022-11-29 16:03:31