Mysql Error 1452 - sqlMessage: "Cannot add or update a child row: a foreign key constraint fails

206 Views Asked by At

I am getting this error, I am trying to assign a a separate Pokemon ID to a type ID through a dropdown menu. Here is the full error

"sqlMessage: "Cannot add or update a child row: a foreign key constraint fails (user.type_has_pokemon, CONSTRAINT fk_type_has_pokemon_pokemon1 FOREIGN KEY (pokemon_pokemonid) REFERENCES pokemon (id) ON DELETE NO ACTION ON UPDATE NO ACTION)", "

Basically I have a pokemon table, a type_has_pokemon to set the type on, and then a type table. Here is my post route

   router.post('/', function(req, res){
                var mysql = req.app.get('mysql');
                var sql = "INSERT INTO type_has_pokemon (pokemon_pokemonid, type_typeid) VALUES (?,?)";
                var inserts = [req.body.pokemon, req.body.type];
                sql = mysql.pool.query(sql, inserts, function(error, results, fields){
                        if(error){
                                res.write(JSON.stringify(error));
                                res.end();
                                } else{
                                        res.redirect('/typehaspokemon');
                        }
                });
        });

Here is my posting form

<form id="addtypepokemon" action="/typehaspokemon" method="post">
Pokemon: <select name="pokemon">
        {{#each pokemon}}
        <option value="{{id}}">{{pokemonname}}</option>
        {{/each}}
        </select><br>
Type: <select name="type">
        {{#each type}}
        <option value="{{typeid}}">{{typename}}</option>
        {{/each}}
        </select><br>
        <input type="submit" value="Submit">
        </form>
</table>
0

There are 0 best solutions below