因此,我编写了一个CakePHP 3.3应用程序,该应用程序具有“Users”表和“UserProfiles”表,并且我不能为我的生活弄清楚为什么当我从我的项目中传输项目时,代码停止工作个人发展环境到我们的托管服务器。我在使用最新的MySQL 5.7,现在我的主机报告我的数据库是Percona 5.5.51-38.2-log。实质上,我相信UserProfile无法保存,因为它找不到创建的用户。我收到的保存错误是:调试CakePHP保存
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`staging`.`user_profiles`, CONSTRAINT `fk_user_profiles_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
我记录的查询和我跑最后一棒显示logs\debug.log
下面的语句:
2016-11-22 23:13:40 Debug: duration=1 rows=1 SELECT Users.id AS `Users__id`, Users.email AS `Users__email`, Users.password AS `Users__password`, Users.role AS `Users__role`, Users.created AS `Users__created`, UserProfiles.user_id AS `UserProfiles__user_id`, UserProfiles.address_one AS `UserProfiles__address_one`, UserProfiles.address_two AS `UserProfiles__address_two`, UserProfiles.age AS `UserProfiles__age`, UserProfiles.sex AS `UserProfiles__sex`, UserProfiles.first_name AS `UserProfiles__first_name`, UserProfiles.last_name AS `UserProfiles__last_name`, UserProfiles.city AS `UserProfiles__city`, UserProfiles.state AS `UserProfiles__state`, UserProfiles.zip AS `UserProfiles__zip`, UserProfiles.phone AS `UserProfiles__phone`, UserProfiles.photo AS `UserProfiles__photo` FROM users Users LEFT JOIN user_profiles UserProfiles ON Users.id = (UserProfiles.user_id) LIMIT 20 OFFSET 0
2016-11-22 23:13:40 Debug: duration=0 rows=1 SELECT (COUNT(*)) AS `count` FROM users Users LEFT JOIN user_profiles UserProfiles ON Users.id = (UserProfiles.user_id)
2016-11-22 23:13:44 Debug: duration=0 rows=0 BEGIN
2016-11-22 23:13:44 Debug: duration=0 rows=0 SELECT 1 AS `existing` FROM users Users WHERE Users.email = '[email protected]' LIMIT 1
2016-11-22 23:13:44 Debug: duration=0 rows=1 INSERT INTO users (email, password, role) VALUES ('[email protected]', 'y$Gak.CvDbw7Jg8gwbEGBiNeVdj7L8i3xScNOoDBegU7DP5aU6A8Ns2', 'admin')
2016-11-22 23:13:44 Debug: duration=0 rows=0 SELECT 1 AS `existing` FROM user_profiles UserProfiles WHERE UserProfiles.user_id = 11 LIMIT 1
2016-11-22 23:13:44 Debug: duration=0 rows=1 SELECT 1 AS `existing` FROM users Users WHERE Users.id = 11 LIMIT 1
2016-11-22 23:13:44 Debug: duration=0 rows=0 INSERT INTO user_profiles (user_id, address_one, address_two, age, sex, first_name, last_name, city, state, zip, phone, photo) VALUES (11, 'Test Place 42', 'Teeeeeeest', 24, 'm', 'Kyle', 'Person', 'Place', 'FL', '12312', '1231231234', '')
2016-11-22 23:13:44 Debug: duration=0 rows=0 ROLLBACK
所以,看来INSERT INTO user_profiles
语句正确选择(在这种情况下为11),但是我在互联网上读到的所有内容都表示它可能无法通过id 11找到用户。CakePHP在我的***中非常痛苦,并且隐藏了可变数据代码从我的错误,说我的SQL查询是:
INSERT INTO user_profiles (user_id, address_one, address_two, age, sex, first_name, last_name, city, state, zip, phone, photo) VALUES (:c0, :c1, :c2, :c3, :c4, :c5, :c6, :c7, :c8, :c9, :c10, :c11)
因此,如果有人知道如何阅读这些“c变量”,那么我会想象一下,这将有很大的帮助。我不擅长调试,所以请随时推荐我可以用来提高生产力的任何方法。
这是构建两个表的SQL代码:
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `staging` DEFAULT CHARACTER SET utf8 ;
USE `staging` ;
DROP TABLE IF EXISTS `staging`.`users` ;
CREATE TABLE IF NOT EXISTS `staging`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`role` ENUM('admin', 'vet', 'client') NOT NULL,
PRIMARY KEY (`id`));
DROP TABLE IF EXISTS `staging`.`user_profiles` ;
CREATE TABLE IF NOT EXISTS `staging`.`user_profiles` (
`user_id` INT NOT NULL,
`address_one` VARCHAR(255) NULL,
`address_two` VARCHAR(255) NULL,
`age` INT NULL,
`sex` ENUM('m', 'f') NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`city` VARCHAR(45) NULL,
`state` VARCHAR(45) NULL,
`zip` VARCHAR(6) NULL,
`phone` VARCHAR(45) NULL,
`photo` VARCHAR(255) NULL,
PRIMARY KEY (`user_id`),
INDEX `fk_user_profiles_users2_idx` (`user_id` ASC),
CONSTRAINT `fk_user_profiles_users`
FOREIGN KEY (`user_id`)
REFERENCES `staging`.`users` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET [email protected]_SQL_MODE;
SET [email protected]_FOREIGN_KEY_CHECKS;
SET [email protected]_UNIQUE_CHECKS;
这是由MySQLWorkbench产生。
非常感谢你的时间和帮助!
这不是问题的答案,请尝试评论或编辑。 –