简体   繁体   English

MySQL 具有 NULL 值的外键

[英]MySQL Foreign key with NULL values

While creating foreign key tables I was expecting foreign key values to be integer type since foreign key references pk values which are integer type.在创建外键表时,我期望外键值是 integer 类型,因为外键引用的 pk 值是 integer 类型。 Could you tell me if it is normal behavior and how to get integer type for foreign key values?您能否告诉我这是否是正常行为以及如何获取外键值的 integer 类型?

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)

Many thanks,非常感谢,

I understood that foreign key NULL values problem (since integer type values were expecting) come from that tables were created with 'index=False' option instead of 'True' statement.我知道外键 NULL 值问题(因为 integer 类型值是预期的)来自使用“index = False”选项而不是“True”语句创建的表。 If no index created in tables, then referencing gets impossible and causes NULL values.如果没有在表中创建索引,则无法进行引用并导致 NULL 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM