I'm still new to understanding relationships and the MVC. But I tried to follow everything I've read online and still receives this error.
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`guest_log`.`logs`, CONSTRAINT `fk_logs_guests1` FOREIGN KEY (`guest_id`) REFERENCES `guests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Here is the SQL for guest, log and department
DROP TABLE IF EXISTS `Dept`;
CREATE TABLE `Dept` (
`dept_id` int(5) NOT NULL AUTO_INCREMENT,
`dept_code` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
`dept_description` varchar(75) CHARACTER SET latin1 NOT NULL,
`dept_assign` enum('Academic','Admin') CHARACTER SET latin1 NOT NULL DEFAULT 'Academic',
`is_deleted` enum('Yes','No') CHARACTER SET latin1 DEFAULT 'No',
PRIMARY KEY (`dept_id`)
)
-- ----------------------------
-- Table structure for guests
-- ----------------------------
DROP TABLE IF EXISTS `guests`;
CREATE TABLE `guests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`middle_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`gender` varchar(6) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tel_no` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`mobile_no` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`company` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`photo` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ----------------------------
-- Table structure for logs
-- ----------------------------
DROP TABLE IF EXISTS `logs`;
CREATE TABLE `logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guest_id` int(11) NOT NULL,
`guest_id_number` datetime NOT NULL,
`date_time_in` datetime NOT NULL,
`date_time_out` datetime DEFAULT NULL,
`purpose` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL,
`department_to_visit` int(11) DEFAULT NULL,
PRIMARY KEY (`id`,`guest_id`),
KEY `fk_logs_guests1_idx` (`guest_id`),
KEY `fk_logs_Dept1_idx` (`department_to_visit`),
CONSTRAINT `fk_logs_guests1` FOREIGN KEY (`guest_id`) REFERENCES `guests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_logs_Dept1` FOREIGN KEY (`department_to_visit`) REFERENCES `Dept` (`dept_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
While here are the models.
class Guest extends Model
{
protected $table = 'guests';
public function logs()
{
return $this->hasMany('App\Log');
}
}
class Log extends Model
{
protected $table = 'logs';
public function guests()
{
return $this->belongsTo('App\Guest','guest_id','id');
}
public function departments()
{
return $this->belongsTo('App\Department','department_to_visit','dept_id');
}
}
class Department extends Model
{
protected $table = 'dept';
public function logs()
{
return $this->hasMany('App\Log','department_to_visit','dept_id');
}
}
Whenever I try to create a new entry for Log, I received this Integrity Constraint Violation error. Please help me resolved and understand why I'm encountering this problem.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire