python sqlite 外键未在级联上删除
python sqlite foreing keys not deleting on cascade
所以我有 2 个文件,一个客户端和一个数据库 class:
import sqlite3
from os.path import isfile
class Database:
def __init__(self, db_name):
self.db_name = db_name
self.check = self.__check_db()
self.conn = sqlite3.connect(self.db_name, check_same_thread=False)
self.cursor = self.conn.cursor()
if not self.check:
self.__create_db()
def query(self, query, args=()):
self.cursor.execute(query, args)
result = self.cursor.fetchall()
self.conn.commit()
return result
def __check_db(self):
return isfile(self.db_name)
def __create_db(self):
self.cursor.executescript("""
PRAGMA FOREIGN_KEYS = ON;
CREATE TABLE utilizadores (id INTEGER PRIMARY KEY, nome TEXT, senha TEXT);
CREATE TABLE artistas (id INTEGER PRIMARY KEY, id_spotify TEXT, nome TEXT);
CREATE TABLE musicas (id INTEGER PRIMARY KEY, id_spotify TEXT, nome TEXT, id_artista INTEGER, FOREIGN KEY(id_artista) REFERENCES artistas(id) ON DELETE CASCADE);
CREATE TABLE avaliacoes (id INTEGER PRIMARY KEY, sigla TEXT, designacao TEXT);
CREATE TABLE playlists (id_user INTEGER, id_musica INTEGER, id_avaliacao INTEGER, PRIMARY KEY (id_user, id_musica), FOREIGN KEY(id_user) REFERENCES utilizadores(id) ON DELETE CASCADE, FOREIGN KEY(id_musica) REFERENCES musicas(id) ON DELETE CASCADE, FOREIGN KEY(id_avaliacao) REFERENCES avaliacoes(id) ON DELETE CASCADE);
INSERT INTO avaliacoes (id, sigla, designacao) VALUES (1, "M", "Medíocre"), (2, "m", "Mau"), (3, "S", "Suficiente"), (4, "B", "Boa"), (5, "MB", "Muito Boa");
""")
和客户;
from database import Database
db = Database("spotify.db")
db.query("insert into artistas (id_spotify, nome) values (?, ?)", ("1", "The Beatles"))
db.query("insert into musicas (id_spotify, nome, id_artista) values (?, ?, ?)", ("m1", "Hey Jude", 1))
db.query("delete from artistas where id = ?", (1,))
如果我因为外键而执行最后一个查询,它也应该删除音乐,但由于某种原因它没有。我不知道为什么这样做,因为我有 PRAGMA fk = on 并且我在同一个连接上
艺术家 --> 艺术家
音乐 --> 音乐
PRAGMA FOREIGN_KEYS = ON
在 每个连接的基础上运行 这意味着您必须为您获得的每个连接对象设置它(如果您想要它提供的行为,否则不要设置它,因为它可能会降低性能)。
里面Database
class定义:
def set_foreign_keys(self)
self.conn.execute("PRAGMA FOREIGN_KEYS = ON")
并在客户端使用:
from database import Database
db = Database("spotify.db")
db.set_foreign_keys()
db.query("insert into artistas (id_spotify, nome) values (?, ?)", ("1", "The Beatles"))
db.query("insert into musicas (id_spotify, nome, id_artista) values (?, ?, ?)", ("m1", "Hey Jude", 1))
db.query("delete from artistas where id = ?", (1,))
所以我有 2 个文件,一个客户端和一个数据库 class:
import sqlite3
from os.path import isfile
class Database:
def __init__(self, db_name):
self.db_name = db_name
self.check = self.__check_db()
self.conn = sqlite3.connect(self.db_name, check_same_thread=False)
self.cursor = self.conn.cursor()
if not self.check:
self.__create_db()
def query(self, query, args=()):
self.cursor.execute(query, args)
result = self.cursor.fetchall()
self.conn.commit()
return result
def __check_db(self):
return isfile(self.db_name)
def __create_db(self):
self.cursor.executescript("""
PRAGMA FOREIGN_KEYS = ON;
CREATE TABLE utilizadores (id INTEGER PRIMARY KEY, nome TEXT, senha TEXT);
CREATE TABLE artistas (id INTEGER PRIMARY KEY, id_spotify TEXT, nome TEXT);
CREATE TABLE musicas (id INTEGER PRIMARY KEY, id_spotify TEXT, nome TEXT, id_artista INTEGER, FOREIGN KEY(id_artista) REFERENCES artistas(id) ON DELETE CASCADE);
CREATE TABLE avaliacoes (id INTEGER PRIMARY KEY, sigla TEXT, designacao TEXT);
CREATE TABLE playlists (id_user INTEGER, id_musica INTEGER, id_avaliacao INTEGER, PRIMARY KEY (id_user, id_musica), FOREIGN KEY(id_user) REFERENCES utilizadores(id) ON DELETE CASCADE, FOREIGN KEY(id_musica) REFERENCES musicas(id) ON DELETE CASCADE, FOREIGN KEY(id_avaliacao) REFERENCES avaliacoes(id) ON DELETE CASCADE);
INSERT INTO avaliacoes (id, sigla, designacao) VALUES (1, "M", "Medíocre"), (2, "m", "Mau"), (3, "S", "Suficiente"), (4, "B", "Boa"), (5, "MB", "Muito Boa");
""")
和客户;
from database import Database
db = Database("spotify.db")
db.query("insert into artistas (id_spotify, nome) values (?, ?)", ("1", "The Beatles"))
db.query("insert into musicas (id_spotify, nome, id_artista) values (?, ?, ?)", ("m1", "Hey Jude", 1))
db.query("delete from artistas where id = ?", (1,))
如果我因为外键而执行最后一个查询,它也应该删除音乐,但由于某种原因它没有。我不知道为什么这样做,因为我有 PRAGMA fk = on 并且我在同一个连接上
艺术家 --> 艺术家
音乐 --> 音乐
PRAGMA FOREIGN_KEYS = ON
在 每个连接的基础上运行 这意味着您必须为您获得的每个连接对象设置它(如果您想要它提供的行为,否则不要设置它,因为它可能会降低性能)。
里面Database
class定义:
def set_foreign_keys(self)
self.conn.execute("PRAGMA FOREIGN_KEYS = ON")
并在客户端使用:
from database import Database
db = Database("spotify.db")
db.set_foreign_keys()
db.query("insert into artistas (id_spotify, nome) values (?, ?)", ("1", "The Beatles"))
db.query("insert into musicas (id_spotify, nome, id_artista) values (?, ?, ?)", ("m1", "Hey Jude", 1))
db.query("delete from artistas where id = ?", (1,))