SQLAlchemy & Sqlite3. sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError)
Есть таблица Products в sqlite. Файл с данными дополняется новыми и изменяется в существующих строках. В файле примерно 28 000 строк. При этом, если я сокращаю количество строк до 500, то все работает нормально.
Помогите разобраться, почему при работе с полным файлом выдает ошибку:
[SQL: INSERT INTO "Products" ("KSSS_prod", "Product_Name", "Pack", "Pack_group", "Brand", "Production", "Segment", "LoB", "BO_type", "Pack_group2", "Analog_LLK", "ED_type", is_deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: (('1000127', 'nan', 'non-ED', '-', '-', '-', '-', '-', '-', '-', '-', '-', 0), ('10004', 'Налив', 'non-ED', '-', '-', '-', '-', '-', '-', '-', '-', '-', 0))]
(Background on this error at: https://sqlalche.me/e/14/gkpj)
Это модель таблицы:
class Products(Base):
__tablename__ = 'Products'
KSSS_prod = Column(Integer, primary_key=True, )
Product_Name = Column(String)
Pack = Column(String)
Pack_group = Column(String)
Brand = Column(String)
Production = Column(String, nullable=True)
Segment = Column(String, nullable=True)
LoB = Column(String, nullable=True)
BO_type = Column(String, nullable=True)
Pack_group2 = Column(String, nullable=True)
Analog_LLK = Column(String, nullable=True)
ED_type = Column(String, nullable=True)
is_deleted = Column(Boolean, default=False)
def __repr__(self):
return f'KSSS prod: {self.KSSS_prod}, Product Name: {self.Product_Name}'
Это функция добавления новых записей:
def create_Products(data):
prod_for_create = []
for row in data:
material_exists = db.query(Products).filter(Products.KSSS_prod == row['KSSS_prod']).count()
if material_exists == 0:
material_list = {'KSSS_prod' : row['KSSS_prod'],
'Product_Name' : row['Product_Name'],
'Pack' : row['Pack'],
'Pack_group' : row['Pack_group'],
'Brand' : row['Brand'],
'Production' : row['Production'],
'Segment' : row['Segment'],
'LoB' : row['LoB'],
'BO_type' : row['BO_type'],
'Pack_group2' : row['Pack_group2'],
'ED_type' : row['ED_type'],
'Analog_LLK' : row['Analog_LLK']}
prod_for_create.append(material_list)
db.bulk_insert_mappings(Products, prod_for_create)
try:
db.commit()
except SQLAlchemyError as e:
print_error(row, "Ошибка целостности данных: {}", e)
db.rollback()
raise
except ValueError as e:
print_error(row, "Неправильный формат данных: {}", e)
db.rollback()
raise
return prod_for_create
Источник: Stack Overflow на русском