Feb-20-2023, 07:34 PM
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:
Here is my __init.py__ file:
Thanks.
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 routesI am really glad the db is working, but does anyone know why I'm getting this error and how I can solve it?
Thanks.