# CakePHP 4: 'Column' cannot be null, but default value defined in database

**URL:** <https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517>\
**Category:** Need Help\
**Tags:** help\
**Created:** [October 16, 2020, 5:41pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517 "2020-10-16T17:41:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 16, 2020, 5:41pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/1 "2020-10-16T17:41:24Z")

</div>

I have a number of non-nullable fields in my database, for which I have provided default values. For example:

```
invest_poc_id int(11) NOT NULL DEFAULT 0

```

When I debug in my `edit` method, I see the default value upon page load.

```
'invest_poc_id' => (int) 0`

```

Field is marked dirty after post.

However, upon POST, the value is `null` and the default value of the field is ignored. I don’t want to make this field nullable, nor do I want to disable strict mode in my application. What can I do to make my app recognize the default value of these fields?

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [October 17, 2020, 5:57am UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/2 "2020-10-17T05:57:37Z")

</div>

This isn’t the answer to your question, but more of a design approach. You’re mixing your technologies, in that using CakePHP for some default fields, and your Database for others. So I would be setting all default values in the CakePHP, in the `Model/table`. That way I only have one place to look and to check for those fields value.

A classic example is the timestamp where in your table you put created and modified fields, but you use your `initialize(}` `$this->addBehavior('Timestamp');` for the “default” values. This allows your app to work on different database paradigms, and someone else if they ever need to maintain it should follow your thinking.

But, as your question asks, I don’t know why it wouldn’t pull the table’s defaults - maybe track down the INSERT query CakePHP is creating to see if it is populating those fields will a null value.

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 19, 2020, 12:24pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/3 "2020-10-19T12:24:49Z")

</div>

Upon form submission, debug shows that the value of the field is null instead of 0. I even have `default => 0` configured on the form control and I still get the error.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 19, 2020, 1:10pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/4 "2020-10-19T13:10:59Z")

</div>

Any chance that this is just a result of not clearing your ORM cache?

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [October 19, 2020, 1:18pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/5 "2020-10-19T13:18:49Z")

</div>

Ah Zuluru beat me to it! If you changed your table structure it’ll be the cache. Also check your spelling of variable/field name is correct.

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 19, 2020, 2:05pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/6 "2020-10-19T14:05:32Z")

</div>

Clearing the cache using `cake cache clear_all` didn’t help. Should I try something else?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 19, 2020, 2:07pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/7 "2020-10-19T14:07:11Z")

</div>

Just sometimes seems like 50% of the posts on here could be skipped if there was a “did you clear your ORM cache?” question before posting. 🙂

The cache clear command doesn’t always work for me, make sure that `tmp/cache/models` is empty after doing it.

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 19, 2020, 2:16pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/8 "2020-10-19T14:16:15Z")

</div>

I logged out of my app, closed/reopened the browser, ran `cake cache clear_all` again, looked in `tmp/cache/models` and confirmed that the directory was empty. Problem persists. No matter what I do, Cake always makes that field null instead of 0 on submit if it doesn’t have a value.

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [October 19, 2020, 10:25pm UTC](https://discourse.cakephp.org/t/cakephp-4-column-cannot-be-null-but-default-value-defined-in-database/8517/9 "2020-10-19T22:25:17Z")

</div>

I know this is a big ask, but maybe we are at the stage where you can put together a small project which duplicates this behaviour? Just that table and its controller/model/view files; so with the problem running and with full source the solution will be found. You may even find what is causing it by putting that demo together. Like, it could be a bug in the validator or anything! If you host it do not turn on debugging!
