0

I am using sqlalchemy my backend DB is Mysql. while inserting data in bulk, i am not able to skip the conflict. My ocde:

from sqlalchemy.orm import Session
from sqlalchemy import insert

from app.config import config
from app.datastores.mysql.db import get_db
from app.datastores.mysql.models import models, schemas

async def adding_cronjob_alerts(
    alert: schemas.CreateAlert, generate_uuid, db: Session = Depends(get_db)
):
    logger.debug(f"[{generate_uuid}] - Customer Alert Doesn't Exist In DB => Storing")
    new_alerts = []
    for to_be_saved_alerts in alert:
        new_alert = models.ReaqtaAlerts(**to_be_saved_alerts.dict())
        new_alerts.append(new_alert)
    logger.debug(f"[{generate_uuid}] - Adding Customer Alerts Record To DB")
    db.add_all(new_alerts)
    logger.debug(f"[{generate_uuid}] - Alerts Added Successfully")
    logger.debug(f"[{generate_uuid}] - Committing DB Session To MySQL Database")
    db.commit()
    logger.debug(f"[{generate_uuid}] - {CLOSING_DB_SESSION_STRING}")
    db.close()
  • @snakecharmerb the code i pasted this is only inserting the alerts but giving error when there is a conflict, i wanted to know what is the solution for this the answers i found all over is for postgresql but nothing for mysql – Sreejit Banerjee Sep 23 '22 at 11:44
  • The more recent answers on the linked duplicate should work for you (sort them by trending). Most of the use SQLAlchemy Core rather than the ORM, which is [recommended](https://docs.sqlalchemy.org/en/14/orm/persistence_techniques.html?highlight=bulk#comparison-to-core-insert-update-constructs) if you are doing bulk inserts. – snakecharmerb Sep 23 '22 at 11:52

0 Answers0