Python Forum
Flask error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint
#1
Hi all,

I have been trying to built a web app that's a book store for ecommerce website. I finally think I got the SQLAlchemy db working. Items and Users are being added to it okay.

However, even though I am adding the content to the db, I am still getting this error:

Quote:sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: book.title
[SQL: INSERT INTO book (title, author, price) VALUES (?, ?, ?)]
[parameters: ('Thinking Fast and Slow', 'Daniel Kahneman', '15')]


Here is my __init.py__ file:

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SECRET_KEY'] = 'c59a0b2727fed8c35a2f3312'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bookden.db'
Bootstrap(app)
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), nullable=False)
    email = db.Column(db.String(50), nullable=False, unique=True)
    password = db.Column(db.String(80), nullable=False)

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(60), nullable=False, unique=True)
    author = db.Column(db.String(60), nullable=False)
    price = db.Column(db.String(), nullable=False)

app.app_context().push()
#db.create_all()
book3 = Book(title='Thinking Fast and Slow', author='Daniel Kahneman', price='15')
db.session.add(book3)
db.session.commit()

from bookden import routes
I am really glad the db is working, but does anyone know why I'm getting this error and how I can solve it?

Thanks.
Reply
#2
Is the entry already in the database?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Hi,

if the unique constrain fails it typcially means that there is already an entry with the exactly same title. As the title has to be unique, you cannot add another book with the same title. This will happen when you run your codes shown twice.

I would also suggest reconsidering making the title alone unique, as it prevents to have two books with the same title but from different authors in the database. You may want to consider to have a combined unique constrain so that title + author have to be unique.

You also may want to rethink your database design for the book table, as it doesn't take under consideration that a book may be written by two or more authors.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,118 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,694 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Server Error with parse_args() using Flask NoNameoN 0 1,097 Jul-30-2022, 09:42 AM
Last Post: NoNameoN
  [split] flask 500 internal server error DarthTensor 3 4,025 Nov-11-2021, 06:10 AM
Last Post: DarthTensor
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 3,338 Apr-15-2021, 10:29 PM
Last Post: atindra
  Flask migrate sqlalchemy not found TomasAm 2 3,504 Dec-01-2020, 10:04 AM
Last Post: TomasAm
  sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) py_kt 3 5,899 Aug-28-2020, 04:47 PM
Last Post: Larz60+
  TemplateNotFound error with flask Veztar 4 18,827 Aug-28-2020, 07:02 AM
Last Post: Veztar
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 4,006 Aug-12-2020, 07:24 PM
Last Post: Alpy
  Flask-Sqlalchemy count products in specific category imawesome 2 29,187 Mar-12-2020, 08:14 PM
Last Post: imawesome

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020