0

I'm using flask-sqlalchemy and I'm trying to follow sqlalchemy syntax v2.0 as much as possible. I'm using PostgreSQL.

I have found some suggestions about "merge/overwrite duplicates", but they don't address my use case. I am more interested in applying something like PostgreSQL' "ON CONFLICT DO NOTHING"

# model
from src.models import db

flask_json = db.dialects.postgresql.JSON


class RawData(db.Model):  # type: ignore
    __tablename__ = "rawdata"
    id_ = db.Column("id", db.String, primary_key=True)
    data = db.Column(flask_json, nullable=False)

somewhere else I have something like this

raw = RawData(id_=track["id"], data=track)
db.session.add(raw)
db.session.commit()

and I'm getting this error

sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "rawdata_pkey"
DETAIL:  Key (id)=(71dmuXNeiMCMgfrTFdS2QL) already exists.

my question is what should I do so that my code ignore those duplicate and keep adding more data???

thanks in advance!

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
juanp_1982
  • 917
  • 2
  • 16
  • 37

0 Answers0