How to fix IntegrityError: FOREIGN KEY constraint fails when deleting record... in Python

Опубликовано: 05 Август 2026
на канале: ORGVSM - Bedroom Playlist & Mix
30
0

Hello, Dedicated Coders! 🖥️💡

We're excited to share with you our newest video, "How to solve IntegrityError: FOREIGN KEY constraint fails when deleting records linked via foreign keys in related tables. in Python". 🎥 This series is meticulously designed to arm you with knowledge 🧠 and skills 🛠️ to overcome frequent coding challenges.

Today, we will decipher 🔎 and resolve a common error faced by Python coders: the bit hard to solve IntegrityError: FOREIGN KEY constraint fails when deleting records linked via foreign keys in related tables.. Here is a snapshot of the code of the video:

Troubling Scenario: ❗️

import sqlite3

Create a database in RAM
db = sqlite3.connect(':memory:')
cursor = db.cursor()

Create tables
cursor.execute('''CREATE TABLE parent (id INTEGER PRIMARY KEY, name TEXT)''')
cursor.execute('''CREATE TABLE child (id INTEGER PRIMARY KEY, parent_id INTEGER, name TEXT,
FOREIGN KEY(parent_id) REFERENCES parent(id))''')

Insert data
cursor.execute('''INSERT INTO parent (id, name) VALUES (1, 'Parent1')''')
cursor.execute('''INSERT INTO child (id, parent_id, name) VALUES (1, 1, 'Child1')''')
db.commit()

Attempt to delete parent record while child record still exists
cursor.execute('''DELETE FROM parent WHERE id = 1''')
db.commit()

db.close()


Unwanted Result: 🚫
IntegrityError: FOREIGN KEY constraint fails when deleting records linked via foreign keys in related tables.

Effective Resolution: ✔️

import sqlite3

Create a database in RAM
db = sqlite3.connect(':memory:')
cursor = db.cursor()

Enable foreign key constraint
db.execute("PRAGMA foreign_keys = ON")

Create tables
cursor.execute('''CREATE TABLE parent (id INTEGER PRIMARY KEY, name TEXT)''')
cursor.execute('''CREATE TABLE child (id INTEGER PRIMARY KEY, parent_id INTEGER, name TEXT,
FOREIGN KEY(parent_id) REFERENCES parent(id))''')

Insert data
cursor.execute('''INSERT INTO parent (id, name) VALUES (1, 'Parent1')''')
cursor.execute('''INSERT INTO child (id, parent_id, name) VALUES (1, 1, 'Child1')''')
db.commit()

Delete child record first
cursor.execute('''DELETE FROM child WHERE parent_id = 1''')

Then delete parent record
cursor.execute('''DELETE FROM parent WHERE id = 1''')
db.commit()

db.close()


Desired Output: 🏁
No output.

In this detailed walkthrough, we will illuminate 💡 the underlying cause of this error, and offer a comprehensive explanation: Code1 fails due to attempting to delete a parent record with an existing child due to foreign key constraints. Code2 resolves this by deleting child records first, ensuring referential integrity. 🎯

Ready to demystify the NameError: name is not defined in your code? Click to watch the video now 🎬. If it aids you in your coding journey, kindly express your appreciation by hitting the like button 👍, and don't hesitate to enrich our coding community by sharing your questions or insights in the comments section 💬.

🔔 Don't miss our upcoming content designed to enhance your coding skills! Subscribe to our channel 📺 and activate notifications – let's keep learning together.
➡️ Click here to subscribe: https://www.youtube.com/@HTFix?sub_co...

Until next time, Happy Coding! 🚀💻

#HowToFix #PythonBug #CodeDebuging #PythonProgramming