# Blog Tutorial / General error: 1364 Field 'category\_id' doesn't have a default value

**URL:** <https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555>\
**Category:** Need Help\
**Created:** [July 26, 2021, 3:14pm UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555 "2021-07-26T15:14:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 26, 2021, 3:14pm UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/1 "2021-07-26T15:14:22Z")

</div>

Hello,  
i am trying to finish the Blog Tutorial of the CakePHP 4.x Strawberry **Cookbook**.  
Unfortunately i am getting an SQLSTATE[HY000]: General error: 1364 Field ‘category\_id’ doesn’t have a default value.

I am at Blog Tutorial - Part 3 right now, but unlike the Tutorials suggests me it is impossible to remove the following controls in the Categories add and edit template files because they do not exist:

echo $this-\>Form-\>control(‘lft’);  
echo $this-\>Form-\>control(‘rght’);

Also it is not possible to disable or remove the requirePresence from the validator for both the lft and rght columns in your CategoriesTable mode because both validators dont exist in the file but I added them:

public function validationDefault(Validator $validator): Validator  
{  
$validator  
-\>add(‘id’, ‘valid’, [‘rule’ =\> ‘numeric’])  
-\>allowEmptyString(‘id’, ‘create’);

```
$validator
    ->add('lft', 'valid', ['rule' => 'numeric'])
// ->requirePresence('lft', 'create')
    ->notEmpty('lft');

$validator
    ->add('rght', 'valid', ['rule' => 'numeric'])
// ->requirePresence('rght', 'create')
    ->notEmpty('rght');

```

}

Thank you for helping!

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [July 27, 2021, 9:57am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/3 "2021-07-27T09:57:07Z")

</div>

I’m not sure, if this Blog Tutorial is a relict from Cake3 and it was just forgotten to remove it from the Cake4 Tutorial.

The easiest way to test the Tree Behavior is to create the tables articles and categories and then use the bake command to create all the code.

Articles Table:

```auto

-- phpMyAdmin SQL Dump
-- version 4.9.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 27. Jul 2021 um 09:55
-- Server-Version: 5.7.32
-- PHP-Version: 7.4.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Datenbank: `blog`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `articles`
--

CREATE TABLE `articles` (
  `id` int(11) NOT NULL,
  `name` varchar(190) NOT NULL,
  `description` text,
  `category_id` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `articles`
--
ALTER TABLE `articles`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `articles`
--
ALTER TABLE `articles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

```

Categories Table:

```auto

-- phpMyAdmin SQL Dump
-- version 4.9.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 27. Jul 2021 um 09:56
-- Server-Version: 5.7.32
-- PHP-Version: 7.4.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Datenbank: `blog`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `categories`
--

CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `name` varchar(190) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `lft` int(11) NOT NULL,
  `rght` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Daten für Tabelle `categories`
--

INSERT INTO `categories` (`id`, `name`, `parent_id`, `lft`, `rght`, `created`, `modified`) VALUES
(1, 'Category 1', NULL, 1, 4, '2021-07-27 09:36:34', '2021-07-27 09:38:10'),
(2, 'Subcategory 1 for Category 1', 1, 2, 3, '2021-07-27 09:38:10', '2021-07-27 09:38:10');

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `categories`
--
ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `categories`
--
ALTER TABLE `categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

```

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 27, 2021, 10:02am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/4 "2021-07-27T10:02:40Z")

</div>

Well, it is possible to add Categories including parents… but it is impossible to add articles because of the General error: 1364 Field ‘category\_id’ doesn’t have a default value.

i will what you suggested now… thank you!

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 27, 2021, 10:42am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/5 "2021-07-27T10:42:14Z")

</div>

both tables are like the ones that were created by the tutorial…

but its not working because of the error

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 27, 2021, 10:47am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/6 "2021-07-27T10:47:37Z")

</div>

CREATE TABLE `articles` (  
`id` int(11) NOT NULL,  
`title` varchar(255) NOT NULL,  
`body` text NOT NULL,  
`category_id` int(11) NOT NULL,  
`created` datetime NOT NULL,  
`modified` datetime NOT NULL  
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

## – – Daten für Tabelle `articles`

INSERT INTO `articles` (`id`, `title`, `body`, `category_id`, `created`, `modified`) VALUES  
(1, ‘The title’, ‘This is the article body.’, 1, ‘2021-07-26 15:04:21’, ‘2021-07-26 15:04:21’);

## – – Indizes der exportierten Tabellen

## – – Indizes für die Tabelle `articles`

ALTER TABLE `articles`  
ADD PRIMARY KEY (`id`);

* * *

CREATE TABLE `categories` (  
`id` int(11) NOT NULL,  
`parent_id` int(11) DEFAULT NULL,  
`lft` int(10) NOT NULL,  
`rght` int(10) NOT NULL,  
`name` varchar(100) NOT NULL,  
`description` varchar(255) NOT NULL,  
`created` datetime NOT NULL,  
`modified` datetime NOT NULL  
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

## – – Daten für Tabelle `categories`

INSERT INTO `categories` (`id`, `parent_id`, `lft`, `rght`, `name`, `description`, `created`, `modified`) VALUES  
(1, NULL, 1, 8, 'Test 1, 'test desc1 ', ‘2021-07-26 12:37:17’, ‘2021-07-26 12:37:17’),  
(2, 1, 2, 3, ‘Test 2’, 'test desc2 ', ‘2021-07-26 12:39:34’, ‘2021-07-26 12:39:34’),  
(3, 1, 4, 5, ‘Test 3’, ‘test desc3’, ‘2021-07-26 12:52:37’, ‘2021-07-26 12:52:37’),  
(4, 1, 6, 7, ‘Test 4’, ‘test desc4’, ‘2021-07-27 10:00:23’, ‘2021-07-27 10:00:23’);

## – – Indizes der exportierten Tabellen

## – – Indizes für die Tabelle `categories`

ALTER TABLE `categories`  
ADD PRIMARY KEY (`id`);

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 27, 2021, 10:53am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/7 "2021-07-27T10:53:48Z")

</div>

**Error** SQLSTATE[HY000]: General error: 1364 Field ‘category\_id’ doesn’t have a default value

- Cake\ORM\Table-\>saveAPP/Controller/ArticlesController php:54](localhost/articles/add#)

APP/Controller/ArticlesController php](phpstorm:open?file=/var/www/html/src/Controller/ArticlesController.php&line=54)

| | ` {` |
| --- | --- |
| | ` $article = $this->Articles->newEmptyEntity();` |
| | ` if ($this->request->is('post')) {` |
| | ` $article = $this->Articles->patchEntity($article, $this->request->getData());` |
| | ` 54: if ($this->Articles->save($article)) {` |
| | ` $this->Flash->success(__('Your article has been saved.'));` |
| | ` return $this->redirect(['action' => 'index']);` |
| | ` }` |
| | ` $this->Flash->error(__('Unable to add your article.'));` |

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 29, 2021, 9:59am UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/9 "2021-07-29T09:59:43Z")

</div>

i just found out how to fix that issue by myself… it was necessary to add ‘category\_id’=\> true into the Article class of the src/Model/Entity/Article.php file…

```
protected $_accessible = [
    'title' => true,
    'body' => true,
    'created' => true,
    'modified' => true,
    'category_id'=> true
];

```

}

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [July 29, 2021, 1:20pm UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/10 "2021-07-29T13:20:09Z")

</div>

In the [tutorial](https://book.cakephp.org/4/en/tutorials-and-examples/cms/database.html#creating-our-first-model), it creates the `src/Model/Entity/Article.php` like this :

```auto
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Article extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'slug' => false,
    ];
}

```

where` '*'=>true` makes all fields accessible.

In the [later stages of the tutorial](https://book.cakephp.org/4/en/tutorials-and-examples/cms/tags-and-users.html), you will use the bake command to create these files

```auto
# Generate all the code at once.
bin/cake bake all tags

```

---

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [July 31, 2021, 6:52pm UTC](https://discourse.cakephp.org/t/blog-tutorial-general-error-1364-field-category-id-doesnt-have-a-default-value/9555/11 "2021-07-31T18:52:51Z")

</div>

blog tutorial… its a different one!
