【问题标题】:MySQL Foreign key with NULL values具有 NULL 值的 MySQL 外键
【发布时间】:2021-12-06 08:13:11
【问题描述】:

在创建外键表时,我期望外键值是整数类型,因为外键引用的是整数类型的 pk 值。 你能告诉我这是否是正常行为以及如何获取外键值的整数类型?

def con_engine():
    url = "mysql+pymysql://{user}:{password}@localhost"
    engine = create_engine(url.format(user='root', password=''))
    engine.execute("CREATE DATABASE IF NOT EXISTS data_immo")
    return


def create_table(d_fr, nam, typ, p_k):
    url = "mysql+pymysql://{user}:{password}@localhost/{db}"
    engine = create_engine(url.format(user='root', password='Fer458it', db='data_immo'))
    d_fr.to_sql(name=nam, con=engine, if_exists='append', index=False, dtype=typ)
    engine.execute(f"ALTER TABLE {name} ADD COLUMN {p_k} INT AUTO_INCREMENT PRIMARY KEY")
    return


d = {'df_Bien_Immo': [df_Bien_Immo, 'Bien_Immo', d_b_i, 'Id'],
     'df_Lot': [df_Lot, 'Lot', d_l, 'Id'],
     'df_Cadastre': [df_Cadastre, 'Cadastre', d_c, 'Id'],
     'df_Mutation': [df_Mutation, 'Mutation', d_c, 'Id'],
     'df_Lot_Adr_Voie': [df_Lot_Adr_Voie, 'Lot_Adr_Voie', d_l_a_v, 'Id'],
     'df_Lot_Adr_Commune': [df_Lot_Adr_Commune, 'Lot_Adr_Commune', d_l_a_c, 'Id'],
     'df_Lot_Adr_Dpt': [df_Lot_Adr_Dpt, 'Lot_Adr_Dpt', d_l_a_d, 'Id']}
con_engine()

for key in d:
    df = d[key][0]
    name = d[key][1]
    d_type = d[key][2]
    p_key = d[key][3]
    create_table(df, name, d_type, p_key)
d_fk = {'Bien_Immo': ['fk_Id_Lot', 'Lot', 'Id'],
        'Lot': [['fk_Id_Bien', 'Bien_Immo', 'Id'],
                ['fk_Id_Cad', 'Cadastre', 'Id'],
                ['fk_Id_mut', 'Mutation', 'Id'],
                ['fk_Id_Voie', 'Lot_Adr_Voie', 'Id']],
        'Cadastre': ['fk_Id_Lot', 'Lot', 'Id'],
        'Mutation': ['fk_Id_Lot', 'Lot', 'Id'],
        'Lot_Adr_Voie': [['fk_Id_Lot', 'Lot', 'Id'],
                         ['fk_Id_Com', 'Lot_Adr_Commune', 'Id']],
        'Lot_Adr_Commune': [['fk_Id_Voie', 'Lot_Adr_Voie', 'Id'],
                            ['fk_Id_Dep', 'Lot_Adr_Dpt', 'Id']],
        'Lot_Adr_Dpt': ['fk_Id_Com', 'Lot_Adr_Commune', 'Id']}


def create_fk(name1, name2, name3, name4):
    url = "mysql+pymysql://{user}:{password}@localhost/{db}"
    engine = create_engine(url.format(user='root', password='Fer458it', db='data_immo'))
    engine.execute(f"ALTER TABLE {name1} ADD COLUMN {name2} int, ADD FOREIGN KEY ({name2}) REFERENCES {name3}({name4}) ON DELETE CASCADE")
    return


for key in d_fk.keys():
    if isinstance(d_fk[key][0], list):
        for i in range(len(d_fk[key])):
            [n2, n3, n4] = [d_fk[key][i][0], d_fk[key][i][1], d_fk[key][i][2]]
            print(f'(n2{n2}, n3{n3}, n4{n4})')
            create_fk(key, n2, n3, n4)
    else:
        [n2, n3, n4] = [d_fk[key][0], d_fk[key][1], d_fk[key][2]]
        print(f'(n2{n2}, n3{n3}, n4{n4})')
        create_fk(key, n2, n3, n4)

非常感谢,

【问题讨论】:

  • 整数列可能有空值,除非声明为非空。您已经向我们倾倒了很多代码,您能否缩小问题发生的范围?如果你把 python 完全排除在你的问题之外可能会更好,因为这是一个 sql 问题,而不是 python 问题。

标签: python mysql foreign-keys


【解决方案1】:

我知道外键 NULL 值问题(因为期望整数类型值)来自表是使用 'index=False' 选项而不是 'True' 语句创建的。如果没有在表中创建索引,则无法进行引用并导致 NULL 值。

【讨论】:

  • 不要发布 cmets/facts 作为答案。编辑问题文本或添加评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多