diff --git a/5_GEONATURE/MIGRATION/RNGL/make_foreignkey.py b/5_GEONATURE/MIGRATION/RNGL/make_foreignkey.py new file mode 100644 index 0000000..2e07e5f --- /dev/null +++ b/5_GEONATURE/MIGRATION/RNGL/make_foreignkey.py @@ -0,0 +1,80 @@ +from sqlalchemy import create_engine # pour lecture de la bd Géonature +from sqlalchemy.engine import URL +from pycen import update_to_sql +import pandas as pd + +usr = 'postgres' +pdw = 'postgres' +bdd = 'serenadb' +host = '172.17.0.2' + +eng = URL.create('postgresql+psycopg2',username=usr,password=pdw,host=host,database=bdd) +conn = create_engine(eng) + +obs = pd.read_sql_table('rnf_obse',con=conn,schema='serenabase') + + + +## Foreign keys table : serenabase.rnf_choi +id_cols = obs.columns[obs.columns.str.endswith('_choi_id')] + +update_to_sql(obs[['obse_id',*id_cols]].replace(0,None).astype('Int64'), + conn,'rnf_obse','serenabase','obse_id') + +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_sexe_fk FOREIGN KEY (obse_sex_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_stade_fk FOREIGN KEY (obse_stade_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_ageunit_fk FOREIGN KEY (obse_ageunit_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_abond_fk FOREIGN KEY (obse_abond_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_precis_fk FOREIGN KEY (obse_precis_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_soci_fk FOREIGN KEY (obse_soci_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_comp_fk FOREIGN KEY (obse_comp_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_compdir_fk FOREIGN KEY (obse_compdir_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_contact_fk FOREIGN KEY (obse_contact_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_contact2_fk FOREIGN KEY (obse_contact2_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_activ_fk FOREIGN KEY (obse_activ_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_caract_fk FOREIGN KEY (obse_caract_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_etat_fk FOREIGN KEY (obse_etat_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_derango_fk FOREIGN KEY (obse_derango_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_derangi_fk FOREIGN KEY (obse_derangi_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_nebul_fk FOREIGN KEY (obse_nebul_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_precip_fk FOREIGN KEY (obse_precip_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_vent_fk FOREIGN KEY (obse_vent_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_ventdir_fk FOREIGN KEY (obse_ventdir_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_visib_fk FOREIGN KEY (obse_visib_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_sol_fk FOREIGN KEY (obse_sol_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_eau_fk FOREIGN KEY (obse_eau_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_choi_eaudir_fk FOREIGN KEY (obse_eaudir_choi_id) REFERENCES serenabase.rnf_choi(choi_id);') + + +id_cols = obs.columns[obs.columns.str.endswith('user_id')] +with conn.begin() as cnx: + cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_user_crea_fk FOREIGN KEY (obse_crea_user_id) REFERENCES serenabase.rnf_user(user_id);') + +# DETAIL: La clé (obse_lmod_user_id)=(bbb76b84-493a-4d26-8f01-0440d377198b) n'est pas présente dans la table « rnf_user ». +# with conn.begin() as cnx: +# cnx.execute('ALTER TABLE serenabase.rnf_obse ADD CONSTRAINT rnf_obse_rnf_user_lmod_fk FOREIGN KEY (obse_lmod_user_id) REFERENCES serenabase.rnf_user(user_id);') + +id_cols = obs.columns[(obs.columns.str.endswith('_id'))& (~obs.columns.str.contains('choi'))] \ No newline at end of file