IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Requêtes MySQL Discussion :

ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails


Sujet :

Requêtes MySQL

  1. #1
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails
    Bonjour,

    J'ai plusieurs tables dans ma base de données mais pour l'exemple, je vais m'arrêter à 3 d'entre elles.
    La base de données stocke des créations artistiques. Pour chaque oeuvre, outre son titre, on a un matériau utilisé (acrylique, fusain, ...) et un type d'oeuvre (dessin, peinture, ...). On a ainsi :
    - 2 tables parentes : "Matter" et "Type"
    - 1 table enfant : "Artwork"

    Elles sont configurées ainsi :
    Matter
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    CREATE TABLE `Matter` (
      `idMatter` int(11) NOT NULL,
      `Matter` varchar(30) COLLATE latin1_general_ci NOT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Matter used in the work';
     
    ALTER TABLE `Matter`
      ADD PRIMARY KEY (`idMatter`);
     
    ALTER TABLE `Matter`
      MODIFY `idMatter` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
    Type
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    CREATE TABLE `Type` (
      `idType` int(11) NOT NULL,
      `Type` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Type of work';
     
    ALTER TABLE `Type`
      ADD PRIMARY KEY (`idType`);
     
    ALTER TABLE `Type`
      MODIFY `idType` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

    Artwork
    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 `Artwork` (
      `id` int(11) NOT NULL,
      `Reference` varchar(9) COLLATE latin1_general_ci NOT NULL,
      `Title` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Type` int(11) DEFAULT NULL,
      `Matter` int(11) DEFAULT NULL,
      `Support` int(11) DEFAULT NULL,
      `Dimensions` int(11) DEFAULT NULL,
      `Theme` int(11) DEFAULT NULL,
      `Filename` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Year` year(4) DEFAULT NULL,
      `Status` int(11) NOT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NULL DEFAULT current_timestamp(),
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      `DateExpiration` timestamp NULL DEFAULT current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Art work';
     
    ALTER TABLE `Artwork`
      ADD PRIMARY KEY (`id`),
      ADD KEY `fk_Matter` (`Matter`),
      ADD KEY `fk_Support` (`Support`),
      ADD KEY `fk_Dimensions` (`Dimensions`);
     
    ALTER TABLE `Artwork`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=96;
    Problème rencontré :
    Tandis que la clé étrangère s'est bien créée dans 'Artwork' pour le champ 'idMatter' de la table 'Matter'. En revanche, c'est pour la clé étrangère du champ 'Type' que je rencontre ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`devel_sh-art`.`#sql-69d_98a`, CONSTRAINT `fk_Type` FOREIGN KEY (`Type`) REFERENCES `Type` (`idType`) ON UPDATE CASCADE)
    .

    J'avais créé la clé étrangère par l'interface graphique de phpmyadmin. Devant l'erreur rencontrée, j'ai ensuite tapé en ligne de commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Type`
    FOREIGN KEY (`Type`) REFERENCES `Type`(`idType`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
    Toujours la même erreur bien sûr.

    Au secours !

  2. #2
    Expert confirmé
    Homme Profil pro
    Responsable Données
    Inscrit en
    Janvier 2009
    Messages
    5 371
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Responsable Données

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5 371
    Par défaut
    Bonjour,
    Citation Envoyé par Shao17 Voir le message
    Tandis que la clé étrangère s'est bien créée dans 'Artwork' pour le champ 'idMatter' de la table 'Matter'.
    Je ne vois pas de définition de clé étrangère dans la création de la table ArtWork (pas de foreign key sur cette colonne).

    Visiblement lors de l'insertion d'une ligne dans la table ArtWork, tu renseignes un Type qui n'existe pas dans la table Type.
    Peux-tu nous montrer la requête d'insertion, ainsi que la ligne de la table Type associée (avec le même IdType) ?

    Tatayo.

  3. #3
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Bonjour Tatayo,

    Je ne suis pas sûr qu'on se soit bien compris. En fait, je ne cherche pas à insérer un enregistrement mais plutôt à créer une clé étrangère.
    La commande que je tape et qui me renvoie l'erreur 1452 est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Type`
    FOREIGN KEY (`Type`) REFERENCES `Type`(`idType`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;

  4. #4
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Je ne comprends pas trop ce qu'il se passe. Je viens de taper ces commandes et ça a fonctionné :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ALTER TABLE `Artwork` ADD KEY `fk_Type` (`Type`);
    Query OK, 0 rows affected (0.085 sec)
    Records: 0  Duplicates: 0  Warnings: 0
     
    ALTER TABLE `Artwork` ADD KEY `fk_Theme` (`Theme`);
    Query OK, 0 rows affected (0.033 sec)
    Records: 0  Duplicates: 0  Warnings: 0
     
    ALTER TABLE `Artwork` ADD KEY `fk_Theme` (`Status`);
    ERROR 1061 (42000): Duplicate key name 'fk_Theme'
    MariaDB [devel_sh-art]> ALTER TABLE `Artwork` ADD KEY `fk_Status` (`Status`);
    Query OK, 0 rows affected (0.038 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    Du coup, j'obtiens cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    SHOW INDEXES FROM `Artwork`;
    +---------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table   | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Artwork |          0 | PRIMARY       |            1 | id          | A         |          95 |     NULL | NULL   |      | BTREE      |         |               |
    | Artwork |          1 | fk_Matter     |            1 | Matter      | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    | Artwork |          1 | fk_Support    |            1 | Support     | A         |          10 |     NULL | NULL   | YES  | BTREE      |         |               |
    | Artwork |          1 | fk_Dimensions |            1 | Dimensions  | A         |          31 |     NULL | NULL   | YES  | BTREE      |         |               |
    | Artwork |          1 | fk_Type       |            1 | Type        | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    | Artwork |          1 | fk_Theme      |            1 | Theme       | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    | Artwork |          1 | fk_Status     |            1 | Status      | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
    +---------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    7 rows in set (0.001 sec)
    Ca a l'air bon.
    Qu'en pensez-vous ?
    Qu'est-ce qui n'allait donc pas dans la 1ère commande ?
    Est-ce que le comportement va être correct lors de requêtes avec jointures ?

  5. #5
    Expert confirmé
    Homme Profil pro
    Responsable Données
    Inscrit en
    Janvier 2009
    Messages
    5 371
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Responsable Données

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5 371
    Par défaut
    En fait je m'étais arrêté au message d'erreur.

    Mais quoi qu'il en soit je vois tout de même un différence entre la requête qui fonctionne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ALTER TABLE `Artwork` ADD KEY `fk_Type` (`Type`);
    et celle qui ne fonctionne pas:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Type`
    FOREIGN KEY (`Type`) REFERENCES `Type`(`idType`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
    La première ne fait qu'ajouter un index, alors que la deuxième ajoute une clé étrangère.
    Ce n'est pas du tout la même chose.

    Dans ton premier message tu indiques que "Tandis que la clé étrangère s'est bien créée dans 'Artwork' pour le champ 'idMatter' de la table 'Matter'.". Non, je ne vois pas de création de clé étrangère, mais juste d'une clé (un index).
    Et idem dans ton dernier message.
    Il n'y a donc aucun lien entre la table Artwork et les autres tables.
    La seule requête qui fait le lien est celle qui ne fonctionne pas.
    Il y a donc des données dans cette table qui ne permettent pas de créer la clé étrangère, des valeurs pour artwork.type qui n'existent pas dans type.idtype.
    Tu peux les retrouver facilement:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    select artwork.id
    from artwork
    left outer join type on type.idtype = artwork.fk_type
    where type.idtype = nul
    Tatayo.

  6. #6
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Merci Tatayo.

    Je croyais en fait que comme apparaissait le fk_... dans la table Artwork pour les 3 clés, ça avait marché. Je trouve ça surprenant qu'il m'ait créé de simples index alors que j'avais renseigné les champs dans le phpmyadmin.

    Donc j'ai essayé ta commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    SELECT Artwork.id FROM Artwork LEFT OUTER JOIN Type ON Type.idType = Artwork.fk_Type WHERE Type.idType = NULL;
    ERROR 1054 (42S22): Unknown column 'Artwork.fk_Type' in 'on clause'
    J'ai corrigé Artwork.fk_Type par le nom de colonne et j'obtiens :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    SELECT Artwork.id FROM Artwork LEFT OUTER JOIN Type ON Type.idType = Artwork.Type WHERE Type.idType = NULL;
    Empty set (0.001 sec)

  7. #7
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Je profite d'un moment de calme pour revenir sur le sujet.
    Au lieu de perdre du temps à essayer de comprendre où peut bien se trouver le problème que tu pointes, Tatayo, j'ai préféré faire un dump de ma base en 4 fichiers SQL afin de recréer ma base de données. Peut-être avais-je créé les tables dans le mauvais ordre ?

    J'ai donc suivi cette procédure :
    1. dump de la structure des tables parentes
    2. dump de la structure de la table enfant (Artwork)
    3. dump des valeurs des tables parentes
    4. dump des valeurs de la table enfant
    5. nettoyage des clés enregistrées dans les dumps
    6. vérification des valeurs enregistrées dans les tables
    7. création du script SQL d'ajout des clés étrangères
    8. suppression de la base de données
    9. import du script SQL de création de la base de données et des tables parentes
    10. import du script SQL de création de la table enfant
    11. import du script SQL d'ajout des clés étrangères
    12. vérification que les clés étrangères sont bien créées dans la table enfant `Artwork`
    13. import des scripts SQL d'ajout des données pour les tables


    Ce qui donne pour les scripts de création de la structure des tables :
    Tables parentes
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:40 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
     
     
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
     
    --
    -- Database: `devel_sh-art`
    --
    CREATE DATABASE IF NOT EXISTS `devel_sh-art` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci;
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Content`
    --
     
    DROP TABLE IF EXISTS `Content`;
    CREATE TABLE IF NOT EXISTS `Content` (
      `idContent` int(11) NOT NULL AUTO_INCREMENT,
      `Menu` int(11) NOT NULL,
      `Label` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Title` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Text` text COLLATE latin1_general_ci DEFAULT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NULL DEFAULT current_timestamp(),
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      `DateExpiration` timestamp NULL DEFAULT current_timestamp(),
      PRIMARY KEY (`idContent`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Text content of the website';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Dimensions`
    --
     
    DROP TABLE IF EXISTS `Dimensions`;
    CREATE TABLE IF NOT EXISTS `Dimensions` (
      `idDimensions` int(11) NOT NULL AUTO_INCREMENT,
      `Dimensions` varchar(50) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idDimensions`)
    ) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Dimensions of the work';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Event`
    --
     
    DROP TABLE IF EXISTS `Event`;
    CREATE TABLE IF NOT EXISTS `Event` (
      `idEvent` int(11) NOT NULL AUTO_INCREMENT,
      `Title` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Teaser` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Place` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Period` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Illustration_1` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Illustration_2` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Alternative_text` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Text` text COLLATE latin1_general_ci DEFAULT NULL,
      `Priority` int(11) DEFAULT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NOT NULL DEFAULT current_timestamp(),
      `DateModification` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
      `DateExpiration` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`idEvent`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Events such as exhibitions';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `GuestBook`
    --
     
    DROP TABLE IF EXISTS `GuestBook`;
    CREATE TABLE IF NOT EXISTS `GuestBook` (
      `id` int(6) NOT NULL AUTO_INCREMENT,
      `Author` varchar(50) COLLATE latin1_general_ci NOT NULL,
      `FromPlace` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
      `EmailAddress` varchar(50) COLLATE latin1_general_ci NOT NULL,
      `Message` text COLLATE latin1_general_ci NOT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NOT NULL DEFAULT current_timestamp(),
      `DateExpiration` timestamp NOT NULL DEFAULT '2030-12-31 22:59:59',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Guest book of the website';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Matter`
    --
     
    DROP TABLE IF EXISTS `Matter`;
    CREATE TABLE IF NOT EXISTS `Matter` (
      `idMatter` int(11) NOT NULL AUTO_INCREMENT,
      `Matter` varchar(30) COLLATE latin1_general_ci NOT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idMatter`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Matter used in the work';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Menu`
    --
     
    DROP TABLE IF EXISTS `Menu`;
    CREATE TABLE IF NOT EXISTS `Menu` (
      `idMenu` int(11) NOT NULL AUTO_INCREMENT,
      `Label` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idMenu`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Website menus';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Status`
    --
     
    DROP TABLE IF EXISTS `Status`;
    CREATE TABLE IF NOT EXISTS `Status` (
      `idStatus` int(11) NOT NULL AUTO_INCREMENT,
      `Status` varchar(50) COLLATE latin1_general_ci NOT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idStatus`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Status of the art work';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Support`
    --
     
    DROP TABLE IF EXISTS `Support`;
    CREATE TABLE IF NOT EXISTS `Support` (
      `idSupport` int(11) NOT NULL AUTO_INCREMENT,
      `Support` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idSupport`)
    ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Support used for the work';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Theme`
    --
     
    DROP TABLE IF EXISTS `Theme`;
    CREATE TABLE IF NOT EXISTS `Theme` (
      `idTheme` int(11) NOT NULL AUTO_INCREMENT,
      `Theme` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idTheme`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Theme of the work';
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Type`
    --
     
    DROP TABLE IF EXISTS `Type`;
    CREATE TABLE IF NOT EXISTS `Type` (
      `idType` int(11) NOT NULL AUTO_INCREMENT,
      `Type` varchar(30) COLLATE latin1_general_ci DEFAULT NULL,
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      PRIMARY KEY (`idType`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Type of work';
     
    COMMIT;
     
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    Table enfant `Artwork`
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:42 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
     
     
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
     
    --
    -- Database: `devel_sh-art`
    --
    CREATE DATABASE IF NOT EXISTS `devel_sh-art` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci;
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Artwork`
    --
     
    DROP TABLE IF EXISTS `Artwork`;
    CREATE TABLE IF NOT EXISTS `Artwork` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `Reference` varchar(9) COLLATE latin1_general_ci NOT NULL,
      `Title` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Type` int(11) DEFAULT NULL,
      `Matter` int(11) DEFAULT NULL,
      `Support` int(11) DEFAULT NULL,
      `Dimensions` int(11) DEFAULT NULL,
      `Theme` int(11) DEFAULT NULL,
      `Filename` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Year` year(4) DEFAULT NULL,
      `Status` int(11) NOT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NULL DEFAULT current_timestamp(),
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      `DateExpiration` timestamp NULL DEFAULT current_timestamp(),
      PRIMARY KEY (`id`),
      KEY `fk_Matter` (`Matter`),
      KEY `fk_Support` (`Support`),
      KEY `fk_Dimensions` (`Dimensions`),
      KEY `fk_Type` (`Type`),
      KEY `fk_Theme` (`Theme`),
      KEY `fk_Status` (`Status`)
    ) ENGINE=InnoDB AUTO_INCREMENT=96 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Art work';
     
    COMMIT;
     
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    Script d'ajout des clés étrangères
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:40 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    --
    -- Database: `devel_sh-art`
    --
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a type
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Type`
    FOREIGN KEY (`Type`) REFERENCES `Type`(`idType`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a matter
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Matter`
    FOREIGN KEY (`Matter`) REFERENCES `Matter`(`idMatter`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a support
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Support`
    FOREIGN KEY (`Support`) REFERENCES `Support`(`idSupport`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a dimension
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Dimensions`
    FOREIGN KEY (`Dimensions`) REFERENCES `Dimensions`(`idDimensions`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a theme
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Theme`
    FOREIGN KEY (`Theme`) REFERENCES `Theme`(`idTheme`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a status
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Status`
    FOREIGN KEY (`Status`) REFERENCES `Status`(`idStatus`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    COMMIT;

    Vérification des clés de la table enfant `Artwork`
    Action ........ Keyname ..... Type ..... Unique ..... Packed ..... Column ..... Cardinality ..... Collation ..... Null ..... Comment
    PRIMARY ....... BTREE ........ Yes ....... No ........ id .......... 0 ............ A ............ No
    fk_Matter ..... BTREE ........ No ........ No ........ Matter ...... 0 ............ A ............ Yes
    fk_Support .... BTREE ........ No ........ No ........ Support ..... 0 ............ A ............ Yes
    fk_Dimensions . BTREE ........ No ........ No ........ Dimensions .. 0 ............ A ............ Yes
    fk_Type ....... BTREE ........ No ........ No ........ Type ........ 0 ............ A ............ Yes
    fk_Theme ...... BTREE ........ No ........ No ........ Theme ....... 0 ............ A ............ Yes
    fk_Status ..... BTREE ........ No ........ No ........ Status ...... 0 ............ A ............ No
    Est-ce que vous pensez que c'est bon comme ça ?

  8. #8
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 542
    Billets dans le blog
    10
    Par défaut
    Un même attribut devrait avoir le même nom dans toutes les tables.

    Par exemple, si l'identifiant de la table MATTER est idmatter alors, dans la table ARTWORK ce même identifiant hérité comme FK devrait aussi s'appeler idmatter sans quoi c'est un véritable foutoir !
    Et par conséquent, la contrainte FK devrait être codée comme suit dans la table ARTWORK : ADD CONSTRAINT `fk_Matter` FOREIGN KEY (`idMatter`) REFERENCES `Matter`(`idMatter`) ON DELETE etc.

  9. #9
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Merci Escartefigue. Je ne suis pas forcément au courant des bonnes pratiques en la matière et il est vrai que j'étais parti pour le nommage des colonnes de la table Artwork.
    Je refais tout ça et vous dis quoi.

  10. #10
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    J'ai donc refait ma table, mes clés étrangères et implanté les données. Mais quand même j'obtiens la fameuse erreur.

    Artwork
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:42 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
     
     
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
     
    --
    -- Database: `devel_sh-art`
    --
    CREATE DATABASE IF NOT EXISTS `devel_sh-art` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci;
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `Artwork`
    --
     
    DROP TABLE IF EXISTS `Artwork`;
    CREATE TABLE IF NOT EXISTS `Artwork` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `Reference` varchar(9) COLLATE latin1_general_ci NOT NULL,
      `Title` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `idType` int(11) DEFAULT NULL,
      `idMatter` int(11) DEFAULT NULL,
      `idSupport` int(11) DEFAULT NULL,
      `idDimensions` int(11) DEFAULT NULL,
      `idTheme` int(11) DEFAULT NULL,
      `Filename` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
      `Year` year(4) DEFAULT NULL,
      `idStatus` int(11) NOT NULL,
      `DateCreation` timestamp NOT NULL DEFAULT current_timestamp(),
      `DatePublication` timestamp NULL DEFAULT current_timestamp(),
      `DateModification` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
      `DateExpiration` timestamp NULL DEFAULT current_timestamp(),
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=96 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Art work';
     
    COMMIT;
     
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    Ajout des clés étrangères
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:40 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    --
    -- Database: `devel_sh-art`
    --
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a type
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Type`
    FOREIGN KEY (`idType`) REFERENCES `Type`(`idType`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a matter
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Matter`
    FOREIGN KEY (`idMatter`) REFERENCES `Matter`(`idMatter`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a support
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Support`
    FOREIGN KEY (`idSupport`) REFERENCES `Support`(`idSupport`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a dimension
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Dimensions`
    FOREIGN KEY (`idDimensions`) REFERENCES `Dimensions`(`idDimensions`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a theme
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Theme`
    FOREIGN KEY (`idTheme`) REFERENCES `Theme`(`idTheme`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    -- --------------------------------------------------------
     
    --
    -- Add a foreign key to table `Artwork` to reference a status
    --
    ALTER TABLE `Artwork`
    ADD CONSTRAINT `fk_Status`
    FOREIGN KEY (`idStatus`) REFERENCES `Status`(`idStatus`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT;
     
    COMMIT;
    Ajout des données dans `Artwork`
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    -- phpMyAdmin SQL Dump
    -- version 4.9.5
    -- https://www.phpmyadmin.net/
    --
    -- Host: localhost
    -- Generation Time: May 08, 2020 at 04:42 PM
    -- Server version: 10.3.22-MariaDB
    -- PHP Version: 7.3.16
     
    --
    -- Database: `devel_sh-art`
    --
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Inserting data for table `Artwork`
    --
     
    TRUNCATE TABLE `Artwork`;
     
    -- --------------------------------------------------------
     
    --
    -- Inserting data for table `Artwork`
    --
     
    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (1, '', 'Vincent Van Gogh', 3, 1, 7, 21, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (2, '', 'Poursuite', 3, 1, 10, 75, 6, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (3, '', 'Isibelle', 3, 1, 10, 65, 5, NULL, 2018, 1, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (4, '', 'Stephen Hawking', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (5, '', 'L'Afghane aux yeux verts', 3, 1, 10, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (6, '', 'Jim Morrison', 3, 1, 10, 69, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (7, '', 'Teenager #001', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (8, '', 'Malcolm X', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (9, '', 'Paul Walker', 3, 1, 10, 24, 5, NULL, 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (10, 'dsc09619', 'Harry Potter', 3, 1, 10, 65, 5, 'DSC09619.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59');
     
    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (11, 'dsc09636', 'Seal', 3, 1, 10, 24, 5, 'DSC09636.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (12, 'dsc09642', 'Michael Jackson', 3, 1, 10, 27, 5, 'DSC09642.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (13, 'dsc09654', 'Sinead O'Connor', 3, 1, 10, 27, 5, 'DSC09654.jpg', 2018, 4, '2019-01-02 16:47:38', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (14, 'dsc09670', 'Chris Martin (Coldplay)', 3, 1, 10, 72, 5, 'DSC09670.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (15, 'dsc09692', 'Johnny Hallyday', 3, 1, 10, 72, 5, 'DSC09692.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (16, 'dsc09706', 'Martin Garrix', 3, 1, 10, 30, 5, 'DSC09706.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (17, 'dsc09708', 'Kungs', 3, 1, 10, 30, 5, 'DSC09708.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (18, 'dsc09720', 'Jimi Hendrix', 3, 1, 10, 42, 5, 'DSC09720.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (19, 'dsc09728', 'Jean-Jacques Goldman', 3, 1, 10, 72, 5, 'DSC09728.jpg', 2018, 4, '2019-01-07 22:31:58', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (20, 'dsc09731', 'Nicola Sirkis (Indochine)', 0, 1, 10, 27, 5, 'DSC09731.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59');
    Erreur :
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`devel_sh-art`.`Artwork`, CONSTRAINT `fk_Type` FOREIGN KEY (`idType`) REFERENCES `Type` (`idType`))
    Pourtant la structure
    (15, 'dsc09692', 'Johnny Hallyday', 3, 1, 10, 72, 5, 'DSC09692.jpg', 2018, 4, '2019-01-07 [...]
    semble conforme aux entrées précédemment acceptées.

    Mais si j'isole l'entrée 15 pour la passer toute seule, elle est alors acceptée et c'est sur le groupe d'entrées suivantes que se produit l'erreur. C'est à dire que toutes les entrées jusqu'à la 15 sont alors écrites dans la table mais pas les entrées de la 16 à la 20.

    Par contre, si je passe les entrées en groupes de 1 à 10 et de 11 à 18 c'est accepté (où est donc passée l'erreur de l'entrée 15 que je n'ai pas modifiée) :
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    USE `devel_sh-art`;
     
    -- --------------------------------------------------------
     
    --
    -- Inserting data for table `Artwork`
    --
     
    TRUNCATE TABLE `Artwork`;
     
    -- --------------------------------------------------------
     
    --
    -- Inserting data for table `Artwork`
    --
     
    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (1, '', 'Vincent Van Gogh', 3, 1, 7, 21, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (2, '', 'Poursuite', 3, 1, 10, 75, 6, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (3, '', 'Isibelle', 3, 1, 10, 65, 5, NULL, 2018, 1, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (4, '', 'Stephen Hawking', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (5, '', 'L'Afghane aux yeux verts', 3, 1, 10, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (6, '', 'Jim Morrison', 3, 1, 10, 69, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (7, '', 'Teenager #001', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (8, '', 'Malcolm X', 3, 1, 7, 104, 5, NULL, 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (9, '', 'Paul Walker', 3, 1, 10, 24, 5, NULL, 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (10, 'dsc09619', 'Harry Potter', 3, 1, 10, 65, 5, 'DSC09619.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59');
     
    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (11, 'dsc09636', 'Seal', 3, 1, 10, 24, 5, 'DSC09636.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (12, 'dsc09642', 'Michael Jackson', 3, 1, 10, 27, 5, 'DSC09642.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (13, 'dsc09654', 'Sinead O'Connor', 3, 1, 10, 27, 5, 'DSC09654.jpg', 2018, 4, '2019-01-02 16:47:38', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (14, 'dsc09670', 'Chris Martin (Coldplay)', 3, 1, 10, 72, 5, 'DSC09670.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (15, 'dsc09692', 'Johnny Hallyday', 3, 1, 10, 72, 5, 'DSC09692.jpg', 2018, 4, '2019-01-07 22:30:45', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (16, 'dsc09706', 'Martin Garrix', 3, 1, 10, 30, 5, 'DSC09706.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (17, 'dsc09708', 'Kungs', 3, 1, 10, 30, 5, 'DSC09708.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (18, 'dsc09720', 'Jimi Hendrix', 3, 1, 10, 42, 5, 'DSC09720.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59');
     
    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (19, 'dsc09728', 'Jean-Jacques Goldman', 3, 1, 10, 72, 5, 'DSC09728.jpg', 2018, 4, '2019-01-07 22:31:58', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (20, 'dsc09731', 'Nicola Sirkis (Indochine)', 0, 1, 10, 27, 5, 'DSC09731.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59');
    Tandis que le groupe des entrées 19 à 20 renvoie la fameuse erreur :
    Error

    SQL query:

    INSERT INTO `Artwork` (`id`, `Reference`, `Title`, `idType`, `idMatter`, `idSupport`, `idDimensions`, `idTheme`, `Filename`, `Year`, `idStatus`, `DateCreation`, `DatePublication`, `DateModification`, `DateExpiration`) VALUES
    (19, 'dsc09728', 'Jean-Jacques Goldman', 3, 1, 10, 72, 5, 'DSC09728.jpg', 2018, 4, '2019-01-07 22:31:58', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59'),
    (20, 'dsc09731', 'Nicola Sirkis (Indochine)', 0, 1, 10, 27, 5, 'DSC09731.jpg', 2018, 4, '2019-01-02 11:55:24', '2019-01-11 22:19:06', '2019-06-05 19:56:55', '2030-12-31 22:59:59')

    MySQL said: Documentation
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`devel_sh-art`.`Artwork`, CONSTRAINT `fk_Type` FOREIGN KEY (`idType`) REFERENCES `Type` (`idType`))

  11. #11
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 542
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 542
    Billets dans le blog
    10
    Par défaut
    Déjà simplifiez vous la vie : le principe des colonnes dont la valeur est attribuée par le SGBD (auto_increment pour MySQL) est de ne pas avoir à s'en soucier.
    Donc, supprimez la colonne "id" et les valeurs associées des ordres INSERT.
    Ensuite vérifiez que les valeurs des FK idType fournies dans l'insert de ARTWORK ont bien été insérées et commitées dans la colonne TYPE.IDTYPE référencée

  12. #12
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Août 2017
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Août 2017
    Messages : 45
    Par défaut
    Bon c'est réglé. Je n'avais pas vu que j'avais des valeurs en dehors de celles admises par les identifiants des tables parentes.

    Merci beaucoup à Tatayo et Escartefigue pour leur aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 13
    Dernier message: 27/08/2015, 17h46
  2. Réponses: 2
    Dernier message: 03/04/2012, 20h00
  3. Cannot add or update a child row: a foreign key constraint fails
    Par dubitoph dans le forum Administration
    Réponses: 4
    Dernier message: 25/01/2012, 08h29
  4. Réponses: 3
    Dernier message: 28/11/2011, 17h26
  5. Réponses: 0
    Dernier message: 12/12/2007, 21h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo