From c2ee55e5e9b82c26e1adf62b04a2ae53a9ed177c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Thu, 10 May 2018 00:12:39 +0300 Subject: [PATCH] Add foreign key constraint to enforce proper parent field in users table --- database.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index d39a95b..383e566 100644 --- a/database.py +++ b/database.py @@ -39,6 +39,7 @@ def add_user(db, *, username, password, email, parent, status): # Add the user into the database cursor = db.cursor() + cursor.execute('PRAGMA foreign_keys = ON;') # Fail if we insert a user with bogus parent field cursor.execute('INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?);', (userid, parent, status, password, username, email, '')) def initialize_users(db, admin_user, admin_password): @@ -58,7 +59,9 @@ def initialize_users(db, admin_user, admin_password): username text NOT NULL, email text NOT NULL, - comment text NOT NULL + comment text NOT NULL, + + FOREIGN KEY(parent) REFERENCES users(id) );''') add_user(db, username = admin_user, password = admin_password, email = '', parent = None, status = userstatus.admin)