В консоли пишет sqlite3.OperationalError: foreign key mismatch - "users" referencing "gang", без использования g_name все нормально, не использовать g_id - та же ошибка.
import sqlite3
with sqlite3.connect("data.db") as db:
qwe = db.cursor()
qwe.execute('''PRAGMA foreign_keys=ON''')
qwe.execute(""" CREATE TABLE IF NOT EXISTS gang
(
id INTEGER,
name TEXT NOT NULL,
PRIMARY KEY (id, name)
);""")
qwe.execute(""" CREATE TABLE IF NOT EXISTS users
(
id INTEGER,
name TEXT NOT NULL,
g_id INTEGER,
g_name TEXT,
PRIMARY KEY (id, g_name)
FOREIGN KEY (g_id) REFERENCES gang (id) ON DELETE CASCADE
FOREIGN KEY (g_name) REFERENCES gang (name) ON UPDATE CASCADE
);""")
gid = input("gid: ")
gname = input("gname: ")
qwe.execute(''' INSERT INTO gang (id, name) VALUES (?, ?) ''', [gid, gname])
db.commit()
uid = input("uid: ")
uname = input("uname: ")
ugid = input("ugid: ")
qwe.execute(''' INSERT INTO users (id, name, g_id, g_name) VALUES (?, ?, ?, ?) ''', [uid, uname, ugid, gname])
db.commit()