简体   繁体   中英

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I use idiorm + slim

I realize the following update query, but I try in the console phpmyadmin and it works.

update empleado set `nieempleado`='11111', nombre="Mickael", apellido1="aaa", apellido2="bbb", email="ma@gmail.com", puesto=0 where id = 1

but in idiorm error SQLSTATE[23000]

What´s the problem?

if(count($error)==0)
        {

            $empleado = ORM::for_table('empleado')->find_one($id);
            $empleado->nieempleado = $nie;
            $empleado->nombre = $nombre;
            $empleado->apellido1 = $apellido1;
            $empleado->apellido2 = $apellido2;
            $empleado->email = $email;
            $empleado->puesto = $puesto;   
            if(!empty($telefono))
            {
                $empleado->telefono = $telefono;
            }
            if($usuario!=='')
            {
                $empleado->usuario_idusuario = $usuario;
            }  
            $empleado->save();
            $app->redirect($app->urlFor('employeeList'));
        }

My tables are usuario:

CREATE TABLE IF NOT EXISTS `usuario` (
           `id` int(11) UNSIGNED AUTO_INCREMENT,
           `username` varchar(45) NOT NULL,
           `contrasenia` varchar(255) NOT NULL,
           `email` varchar(45) NOT NULL,
           `admin` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
            PRIMARY KEY (`id`)) 
            ENGINE = InnoDB;

and empleado

CREATE TABLE IF NOT EXISTS `empleado` (
           `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
           `nieempleado` VARCHAR(10) NOT NULL,
           `nombre` VARCHAR(45) NOT NULL,
           `apellido1` VARCHAR(50) NOT NULL,
           `apellido2` VARCHAR(50) NOT NULL,
           `email` VARCHAR(100) NOT NULL,
           `telefono` VARCHAR(10) NULL,
           `puesto` int(11) NOT NULL,
           `usuario_idusuario` int(11) UNSIGNED,
            PRIMARY KEY (`id`),
            INDEX `fk_empleado_usuario1_idx` (`usuario_idusuario` ASC),
            CONSTRAINT `fk_empleado_usuario_id_fk`
            FOREIGN KEY (`usuario_idusuario`)
            REFERENCES `usuario` (`id`)
            ON DELETE CASCADE
            ON UPDATE CASCADE
            )ENGINE = InnoDB;

I get the following error:

Type: PDOException
Code: 23000
Message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`viversoft`.`empleado`, CONSTRAINT `fk_empleado_usuario_id_fk` FOREIGN KEY (`usuario_idusuario`) REFERENCES `usuario` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
File: C:\wamp\www\viver\vendor\j4mie\idiorm\idiorm.php
Line: 413
Trace

#0 C:\wamp\www\viver\vendor\j4mie\idiorm\idiorm.php(413): PDOStatement->execute(Array)
#1 C:\wamp\www\viver\vendor\j4mie\idiorm\idiorm.php(1735): ORM::_execute('UPDATE `emplead...', Array, 'default')
#2 C:\wamp\www\viver\routes\employees.php(213): ORM->save()
#3 [internal function]: {closure}('1')
#4 C:\wamp\www\viver\vendor\slim\slim\Slim\Route.php(462): call_user_func_array(Object(Closure), Array)
#5 C:\wamp\www\viver\vendor\slim\slim\Slim\Slim.php(1326): Slim\Route->dispatch()
#6 C:\wamp\www\viver\vendor\slim\slim\Slim\Middleware\Flash.php(85): Slim\Slim->call()
#7 C:\wamp\www\viver\vendor\slim\slim\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call()
#8 C:\wamp\www\viver\vendor\slim\slim\Slim\Middleware\PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#9 C:\wamp\www\viver\vendor\slim\slim\Slim\Slim.php(1271): Slim\Middleware\PrettyExceptions->call()
#10 C:\wamp\www\viver\public\index.php(55): Slim\Slim->run()
#11 {main}

Your error means that you're trying to insert a value into the column usuario_idusuario in the empleado table that does not exist in the column id in the usuario table. Without seeing the query that's being run and the table values, I can't give you any more info. You need to ensure any value going into usuario_idusuario exists in id .

Thanks,

Andrew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM