[英]CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1364
I am following the Yii Blog tutorial , and am stuck at this error while doing the comments section.我正在关注Yii 博客教程,在做评论部分时遇到了这个错误。
CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1364 Field 'post_id' doesn't have a default value. The SQL statement executed was: INSERT INTO `tbl_comment` (`status`, `content`, `author`, `email`, `url`, `create_time`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5)
This same error was on status first, but I set the default value for it in the database.同样的错误首先出现在状态上,但我在数据库中为其设置了默认值。 But this time its post_id, I dont know how to work it out.
但这一次是post_id,我不知道如何解决。 Post ID is the FK from another table.
帖子 ID 是来自另一个表的 FK。 Here is the whole database design.
这是整个数据库设计。
Thanks!谢谢!
Looks like your "post_id" attribute, which probably is your primary key, is not set to "auto increment" or is setup with "not null" in your Database.看起来您的“post_id”属性(可能是您的主键)未设置为“自动增量”或在您的数据库中设置为“非空”。 Take a look at the following ORM.
看看下面的ORM。 Compare it with yours and fix your DB-sided error.
将它与您的进行比较并修复您的 DB 端错误。 And don't forget to upgrade you model via.
并且不要忘记通过升级您的模型。 GII!
盖伊!
-- -----------------------------------------------------
-- Table `mydb`.`tbl_comment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tbl_comment` ;
CREATE TABLE IF NOT EXISTS `mydb`.`tbl_comment` (
`post_id` INT NOT NULL,
`status` VARCHAR(45) NULL,
`content` TEXT NULL COMMENT ' ',
`author` VARCHAR(255) NULL,
`email` VARCHAR(255) NULL,
`url` VARCHAR(511) NULL,
`create_time` DATETIME NULL,
PRIMARY KEY (`post_id`))
ENGINE = InnoDB;
Else, if "post_id" is not your primary key and is not set as "auto increment" you can try this to fix it:否则,如果“post_id”不是您的主键并且未设置为“自动增量”,您可以尝试使用以下方法修复它:
Solution 1) Make "post_id" set before save/update in php like:解决方案1)在php中保存/更新之前设置“post_id”,例如:
$model = new Tbl_comment; //hope this is your Yii model name...
$model->post_id = 123
if(!$model->save()) {
var_dump($model->errors);
}
Solution 2) Add a default value in your database ORM on attribute "post_id".解决方案 2) 在您的数据库 ORM 中的属性“post_id”上添加一个默认值。 (Cause i dont know your relation and ORM right.)
(因为我不知道你的关系和 ORM 是对的。)
-----------------------------------------
-- Table `mydb`.`tbl_comment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tbl_comment` ;
CREATE TABLE IF NOT EXISTS `mydb`.`tbl_comment` (
`post_id` INT NULL DEFAULT someDefault,
`status` VARCHAR(45) NULL,
`content` TEXT NULL COMMENT ' ',
`author` VARCHAR(255) NULL,
`email` VARCHAR(255) NULL,
`url` VARCHAR(511) NULL,
`create_time` DATETIME NULL)
ENGINE = InnoDB;
Table details:表详细信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.