avec les clé etrangères de toutes les tables dans "transfer" pour crée le lien ; j'explique la situation, je dois gérer les transfert de matières premiere a travers le monde entre different pays et j'ai un probleme lorsque je veux remplir ma table "transfer" il me dit ceci :
ERREUR sur la ligne 1] Cannot add or update a child row: a foreign key constraint fails ('usda2'.'transfer', CONSTRAINT 'fk_transfer_countries' FOREIGN KEY ('countries_id_countries') REFERENCES 'countries' ('id_countries') ON DELETE CASCADE ON UPDATE CASCADE)
pour chaque ligne...
Mais j'arrive a remplir les autres tables .. celle ou il n'y a pas de clé etrangère, en important les fichier CSV
je n'utilisais pas MySQL avant , mon code de creation des tables est celui-ci :
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS 'usda_new' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE 'usda_new' ;
-- -----------------------------------------------------
-- Table 'usda_new'.'transfert'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'transfert' (
'id_transfert' INT NOT NULL AUTO_INCREMENT,
'values' INT NULL,
PRIMARY KEY ('id_transfert'))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'countries'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'countries' (
'id_countries' INT NOT NULL AUTO_INCREMENT,
'code' VARCHAR(2) NULL,
'name' VARCHAR(45) NULL,
'transfert_id_transfert1' INT NOT NULL,
PRIMARY KEY ('id_countries'),
INDEX 'fk_countries_transfert2_idx' ('transfert_id_transfert1' ASC),
CONSTRAINT 'fk_countries_transfert2'
FOREIGN KEY ('transfert_id_transfert1')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'commodities'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'commodities' (
'id_commodities' INT NOT NULL AUTO_INCREMENT,
'code' INT NULL,
'description' VARCHAR(100) NULL,
'transfert_id_transfert' INT NOT NULL,
PRIMARY KEY ('id_commodities'),
INDEX 'fk_commodities_transfert1_idx' ('transfert_id_transfert' ASC),
CONSTRAINT 'fk_commodities_transfert1'
FOREIGN KEY ('transfert_id_transfert')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'attributes'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'attributes' (
'id_attributes' INT NOT NULL AUTO_INCREMENT,
'description' VARCHAR(100) NULL,
'transfert_id_transfert' INT NOT NULL,
PRIMARY KEY ('id_attributes'),
INDEX 'fk_attributes_transfert1_idx' ('transfert_id_transfert' ASC),
CONSTRAINT 'fk_attributes_transfert1'
FOREIGN KEY ('transfert_id_transfert')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'units'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'units' (
'id_units' INT NOT NULL AUTO_INCREMENT,
'description' VARCHAR(100) NULL,
'transfert_id_transfert' INT NOT NULL,
PRIMARY KEY ('id_units'),
INDEX 'fk_units_transfert1_idx' ('transfert_id_transfert' ASC),
CONSTRAINT 'fk_units_transfert1'
FOREIGN KEY ('transfert_id_transfert')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'market'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'market' (
'id_market' INT NOT NULL AUTO_INCREMENT,
'year' YEAR NULL,
'transfert_id_transfert1' INT NOT NULL,
PRIMARY KEY ('id_market'),
INDEX 'fk_market_transfert2_idx' ('transfert_id_transfert1' ASC),
CONSTRAINT 'fk_market_transfert2'
FOREIGN KEY ('transfert_id_transfert1')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table 'usda_new'.'calendar'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'usda_new'.'calendar' (
'id_calendar' INT NOT NULL AUTO_INCREMENT,
'date' DATE NULL,
'transfert_id_transfert' INT NOT NULL,
PRIMARY KEY ('id_calendar'),
INDEX 'fk_calendar_transfert1_idx' ('transfert_id_transfert' ASC),
CONSTRAINT 'fk_calendar_transfert1'
FOREIGN KEY ('transfert_id_transfert')
REFERENCES 'usda_new'.'transfert' ('id_transfert')
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Je ne comprend pas ce probleme car sous POSTGRES cela ne m'étais jamais arrivé.. je vous remercie d'avance pour votre aide..
j'espere avoir pu etre assez explicite..
Cordialement
A voir également:
ERREUR sur la ligne 1] Cannot add or update a child row: a forei