Search code examples
pythonmysqljinja2flask-sqlalchemy

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: book.isbn when parsing and inserting csv data into database


I'm fairly new to the whole flask/sqlalchemy frameworks so I'd like to ask some help in figuring out what's wrong with my flask application. As far as I can see I've done the primary to foregin key relationships as they should be which is a one to many from Book to Reviews. Please any sort of help would be appreciated thanks in advance! I've inserted my code below for further reference.

main.py

with open('books.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    
    for row in reader:
        book = Book(isbn=row['ISBN'],title=row['Book-Title'], author=row['Book-Author'],
        publication_year=row['Year-Of-Publication'], publisher=row['Publisher'])
        db.session.add(book)
        db.session.commit()


print('database initialized!')

models.py

class Review(db.Model):
  id = db.Column(db.Integer, primary_key=True)      
  text=db.Column(db.String)
  rating=db.Column(db.Integer)     
  book=db.Column(db.String,db.ForeignKey("book.isbn"))

  def toDict(self):
    return {
      "id": self.id,
      "text": self.text,
      "rating": self.rating,
      "isbn": self.isbn
    }

class Book(db.Model):
  isbn=db.Column(db.String, primary_key=True)
  title=db.Column(db.String)
  author=db.Column(db.String)
  publication_year=db.Column(db.String)
  publisher=db.Column(db.String)
  reviews = db.relationship('Review', backref='book_review',lazy=True, cascade="all, delete-orphan")
  
  def toDict(self):
    return {
      "isbn": self.isbn,
      "title": self.title,
      "author": self.author,
      "publication_year": self.publication_year,
      "reviews": self.reviews
    }

Solution

  • Unique constraint failure when inserting into a table happens when you try to insert a row which has a value from a column with UNIQUE attribute conflicts with the existing data which has the identical value. To resolve the issue,if you are inserting into an empty table, you can edit the CSV file and use find or other options to locate the duplicate values. And if the table has existing data, I would suggest creating a temporary table without defining PK/UNIQUE for the said column. Then populate data from both sources. Next, run a statement like this to find the duplicate values:

    select problematic_column, count(problematic_column) ct from temporary_table 
    group by problematic_column having ct>1 ;