Salut à tous,
pouvez-vous me donner un coup de mains parce que je sèche sur un pb!
Je cherche :
- à insérer une commande dans la table "commandes" (là, pas de pb)
- à récupérer l'ID générée par cette insertion parce qu'il est en auto_increment dans la table "commande" (là pas de pb)
- à insérer plusieurs produits dans une deuxième table "lignes_de_commandes" avec ce même ID (impossible).
En effet, j'ai le message :
Table commandes
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Erreur SQL ! insert into lignes_de_commandes values(DEFAULT, 7, 1, 60, 57.62, 294, 351.62) Cannot add or update a child row: a foreign key constraint fails nomBD.lignes_de_commandes`, CONSTRAINT `fkIDCMD_LignesDeCMD` FOREIGN KEY (`IDCommande`) REFERENCES `commandes` (`IDCommande`))
Table : lignes_de_commandes
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 create table commandes( IDCommande int(10) unsigned not null auto_increment, IDClient int(10) unsigned not null, NumeroCommande varchar(20) not null, DateCommande datetime, DateRetrait date not null, IDTypeRetrait int not null, constraint pkIDCommande primary key(IDCommande), constraint fkIDCLCommande foreign key(IDClient) references clients(IDClient), index(IDClient) ) engine = InnoDB
Je vous en remercie d'avance de votre aide!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 create table lignes_de_commandes( IDLignesCmd int(10) unsigned not null auto_increment, IDCommande int(10) unsigned not null, IDProduit int(10) unsigned not null, QteCommandee decimal(10, 2) not null, MontantTVA decimal(8, 2) not null, MontantHT decimal(9, 2) not null, MontantTTC decimal(9, 2) not null, constraint pk_lignes_de_cmd primary key(IDLignesCmd), constraint fkIDProdLignesCmd foreign key(IDProduit) references produits(IDProduit), constraint fkIDCMD_LignesDeCMD foreign key(IDCommande) references commandes(IDCommande), index(IDCommande, IDProduit) ) engine = InnoDB
No
Partager