首页 > 解决方案 > Mariadb:外键始终为 NULL

问题描述

我试图在我的表 s2 中创建外键。我看到我的表 s2 中的列 id_s1 始终为 NULL。我的代码如下:

cursor.execute("CREATE TABLE s1 (
    id binary(8) primary key, Cp VARCHAR(10), 
    name varchar(10),  
    capital VARCHAR(10)); »)
cursor.execute("INSERT INTO s1 (id, Cp, name, capital)
     VALUES (uuid(),%s, %s, %s )", (x1, x2, x3))
cursor.execute("CREATE TABLE s2 (
    id binary(8) primary key, 
    Dpt VARCHAR(10), 
    Nation VARCHAR(10), 
    id_s1 binary(8), 
    FOREIGN KEY(id_s1) REFERENCES s1(id)); »)
cursor.execute("INSERT INTO s2 (id, Dpt, nation)
     VALUES (%s, %s, %s)", (x1, x2, x3))
cnx.commit()

标签: python-3.xmariadbmysql-python

解决方案


推荐阅读