Click here to Skip to main content
15,937,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a small webapp using flask and sqlAlchemy.

Im currently stuck at one problem. The problem is that I can not remove entries from the database using sqlAlchemy. The error wich gets returned:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: attachment.share_id
[SQL: UPDATE attachment SET share_id=? WHERE attachment."ID" = ?]
[parameters: (None, 1)]
(Background on this error at:

What my codes looks like (the sql module):
class Share(db.Model):
    ID = db.Column(db.Integer, primary_key=True)
    id = db.synonym("ID")
    uuid = db.Column(db.String(36), unique=True,
                     nullable=False, default=get_unique_id)
    title = db.Column(db.String(100), nullable=True)
    content = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.ID'), nullable=False)
    attachments = db.relationship(
        "Attachment", backref="share", lazy=True)
    comments = db.relationship(
        "Comment", backref="share", order_by="desc(", lazy=True)

    def __repr__(self):
        return f"<Share('{self.ID}', '{self.title}', '{}')>"

The code where I try to remove the entry:
def delete_share(share_uuid):
    share = Share.query.filter_by(uuid=share_uuid).first()
    if share and == current_user:

    if request.args.get("confirm") == "yes":

        flash("Share successfully deleted.", "flash")
        return redirect(url_for('community'))

    return render_template("community/share/delete.html", share=share, title="Delete Share?")

What I have tried:

I have no Idea where to start so please send me some help...
Updated 24-Feb-20 4:14am

1 solution

The problem you are experiencing looks to be caused by having a relationship between the 2 tables involved (share / attachment) and trying to delete an entry from the table with the PrimaryKey.
DBAPI Errors

Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails.
This error is a DBAPI Error and originates from the database driver (DBAPI)...

What I would do would be to delete the affected rows from the Attachment table before you perform the deletion in the Share table.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900