简体   繁体   English

CDbCommand 执行 SQL 语句失败:SQLSTATE[HY000]:一般错误:1364

[英]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.

相关问题 CDbCommand :: fetchColumn()失败:SQLSTATE [HY000]:常规错误:2014在其他无缓冲查询处于活动状态时无法执行查询 - CDbCommand::fetchColumn() failed: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active :SQLSTATE [HY000]:一般错误:1364 字段 - : SQLSTATE[HY000]: General error: 1364 Field 无法运行查询:SQLSTATE[HY000]:一般错误:1364 字段“类型”没有默认值 - Failed to run query: SQLSTATE[HY000]: General error: 1364 Field 'type' doesn't have a default value SQL 插入到 select 错误:SQLSTATE[HY000]:一般错误:1364 字段''没有默认值 - SQL insert into select Error: SQLSTATE[HY000]: General error: 1364 Field '' doesn't have a default value 错误SQLSTATE [HY000]:常规错误:1364字段“名称”没有默认值(SQL:插入“收藏夹”()值()) - ERROR SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `favorites` () values ()) SQLSTATE [HY000]:一般错误:1364字段“登录”没有默认值(SQL:插入到“用户”()值()中) - SQLSTATE[HY000]: General error: 1364 Field 'login' doesn't have a default value (SQL: insert into `users` () values ()) SQLSTATE [HY000]:常规错误:1364字段“ user_id”没有默认值(SQL - SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL “ SQLSTATE [HY000]:一般错误:1364字段'user_id'没有默认值(SQL:插入 - "SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into SQLSTATE [HY000]:一般错误:1364 字段“名称”没有默认值(SQL:插入“产品”(“特色图像” - SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `products` (`featured_image` SQLSTATE [HY000]:执行查询后出现一般错误 - SQLSTATE[HY000]: General error after execute query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM