2

I am following the Yii Blog tutorial, and am stuck at this error while doing the comments section.

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 is the FK from another table. Here is the whole database design.

Thanks!

2
  • You havent set the post_id value in the insert query. You need to set its value as its a foreign key Commented Sep 1, 2014 at 12:43
  • Here it is, codepad.org/EO50vNeN Commented Sep 1, 2014 at 12:54

1 Answer 1

3

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. Take a look at the following ORM. Compare it with yours and fix your DB-sided error. And don't forget to upgrade you model via. GII!

enter image description here

-- -----------------------------------------------------
-- 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:

Solution 1) Make "post_id" set before save/update in php like:

$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". (Cause i dont know your relation and ORM right.)

enter image description here

-----------------------------------------
-- 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:

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Here is the ER of my schema, i.imgur.com/37H2B1j.png?1 Once I set the default value for post_id, I got the same exception for 'id'. CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value. The SQL statement executed was: INSERT INTO tbl_comment (status, post_id, content, author, email, url, create_time) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)
Where is the field id comes from? You said your error is about post_id. If field id is also not PK/NOTNULL/NODEFAULT, configure field id as PK/NOTNULL/AUTO-INCREMENT?! Its the same you did to post_id make id as primary key and put "autoincrement" on it (field AI in workbench), ok?
@user1502, take a look at the last screenshot i added right now. It shows you how to configure it right.
After doing the AI, I get this, CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.tbl_comment, CONSTRAINT FK_comment_post FOREIGN KEY (post_id) REFERENCES tbl_post (id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_comment (status, post_id, content, author, email, url, create_time) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)
I am sorry, it says you must have 20 reputation to talk.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.