X-Received: by 2002:a5d:6c67:: with SMTP id r7mr21119479wrz.286.1638529105918; Fri, 03 Dec 2021 02:58:25 -0800 (PST) MIME-Version: 1.0 Path: ...!buffer1.nntp.ams1.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 03 Dec 2021 04:58:25 -0600 Newsgroups: fr.comp.lang.python Subject: Fonction Python et MySQL, pour créer une clé étrangère X-poster: PEAR::Net_NNTP v1.5.0 (stable) From: Olivier92410 Organization: !No_Organization! Message-ID: Date: Fri, 03 Dec 2021 04:58:25 -0600 Lines: 31 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-WzgADHnbmGpAEXknA9UX13Y8qjefR7E9U1wCrxhJMXmE+VyNrUllbnH1KYsU2US4Pvs+d/7om0iqklK!y1tAddumZShJI5HNsMzEbPJuY6CJunhxLiWF4JM4GhvjWejlqM1oefl0/dw/Hq4f0p3hSW0+pZDm X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 2720 X-Original-Lines: 30 X-Original-Bytes: 2424 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Bonjour, Je veux insérer dans plusieurs tables une clé étrangère. Pour cela, j'ai créé un dictionnaire qui fournira à une fonction les paramètres nécessaires à la création de ces clés. Ci-dessous, une partie du dictionnaire utilisé, pour rester simple: d_fk = {'Bien_Immo': ['fk_Id_Lot', 'Lot', 'Id']} Puis la fonction, def create_fk(name1, name2, name3, name4): url = "e;mysql+pymysql://{user}:{password}@localhost/{db}"e; engine = create_engine(url.format(user='root', password='Fer458it', db='data_immo')) engine.execute(f"e;ALTER TABLE {name1}"e; f"e;ADD COLUMN {name2},"e; f"e;ADD CONSTRAINT {name2}"e; f"e;ADD FOREIGN KEY ({name2}) REFERENCES {name3}({name4})"e; f"e;ON DELETE CASCADE"e;) 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]] create_fk(key, n2, n3, n4) else: [n2, n3, n4] = [d_fk[key][0], d_fk[key][1], d_fk[key][2]] create_fk(key, n2, n3, n4) Ce code retourne une erreur de syntaxe 1064, Si vous avez des idées, elles sont bienvenues ! Merci