Skip to content

fix modified column being nullable#1026

Open
LordSimal wants to merge 2 commits into6.xfrom
fix-modified-nullable
Open

fix modified column being nullable#1026
LordSimal wants to merge 2 commits into6.xfrom
fix-modified-nullable

Conversation

@LordSimal
Copy link
Contributor

Since we are now more tightly integrating the migrations plugin with the CakePHP framework I'd propose to change the default updated/modified column type to not be nullable, as by the TimestampBehavior this will always be set.

@LordSimal LordSimal added this to the 5.next milestone Feb 21, 2026
}
}
if ($onUpdate !== null && $onUpdate !== '') {
$column->setUpdate($onUpdate);
Copy link
Contributor Author

@LordSimal LordSimal Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markstory is this maybe a bug in the base schema dialect?

It didn't correctly detect the on update CURRENT_TIMESTAMP as it seems - but just for MySQL

Copy link
Contributor Author

@LordSimal LordSimal Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found the issue - the Extra column in that describe query contains

DEFAULT_GENERATED on update CURRENT_TIMESTAMP

as you can see here

  [1] =>
  array(9) {
    'Field' =>
    string(7) "created"
    'Type' =>
    string(8) "datetime"
    'Collation' =>
    NULL
    'Null' =>
    string(2) "NO"
    'Key' =>
    string(0) ""
    'Default' =>
    string(17) "CURRENT_TIMESTAMP"
    'Extra' =>
    string(17) "DEFAULT_GENERATED"
    'Privileges' =>
    string(31) "select,insert,update,references"
    'Comment' =>
    string(0) ""
  }
  [2] =>
  array(9) {
    'Field' =>
    string(7) "updated"
    'Type' =>
    string(8) "datetime"
    'Collation' =>
    NULL
    'Null' =>
    string(2) "NO"
    'Key' =>
    string(0) ""
    'Default' =>
    string(17) "CURRENT_TIMESTAMP"
    'Extra' =>
    string(45) "DEFAULT_GENERATED on update CURRENT_TIMESTAMP"
    'Privileges' =>
    string(31) "select,insert,update,references"
    'Comment' =>
    string(0) ""
  }
mysql> SHOW FULL COLUMNS FROM table1;
+---------+--------------+-----------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+
| Field   | Type         | Collation | Null | Key | Default           | Extra                                         | Privileges                      | Comment |
+---------+--------------+-----------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+
| id      | int unsigned | NULL      | NO   | PRI | NULL              | auto_increment                                | select,insert,update,references |         |
| created | datetime     | NULL      | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             | select,insert,update,references |         |
| updated | datetime     | NULL      | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP | select,insert,update,references |         |
+---------+--------------+-----------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+

@markstory
Copy link
Member

Do we still need all of this with the change made to cake core?

@LordSimal
Copy link
Contributor Author

Nope, will re-check this with latest 5.x

@LordSimal
Copy link
Contributor Author

So the change in cakephp/cakephp#19207 now causes these tests to fail as the serialized schema dumps do not contain that new property.

@markstory @dereuromark I have no idea how those schema-dump files are generated

@LordSimal LordSimal force-pushed the fix-modified-nullable branch from 2e1218f to 74800c7 Compare February 27, 2026 06:51
@markstory
Copy link
Member

I have no idea how those schema-dump files are generated

I usually just update those by hand 😊

@LordSimal LordSimal marked this pull request as ready for review March 1, 2026 09:36
@LordSimal
Copy link
Contributor Author

Thanks to Codex we now have a PHP script to automatically update all the Diff Schema Dump Files instead of having to manually adjust/update those serialized dump files whenever we decide/need to change something in our database class properties.

@LordSimal
Copy link
Contributor Author

So should this be released in a patch release or should this rather go in a feature release?

@dereuromark
Copy link
Member

5.next could be safer, not sure what the implications are for users:

  • Release as a minor version (not patch)
  • Mention in release notes that modified columns are now non-nullable

@dereuromark
Copy link
Member

Concerns

  1. This is a behavior change, not a bug fix. It changes how schemas are generated, which could cause:
    - Schema inconsistency between old and new migrations
    - Issues for anyone using addTimestamps() without TimestampBehavior
  2. Edge case - What about apps that intentionally leave modified as null on first insert and only set it on update? (Rare, but possible.) With this change, even the initial insert would get a timestamp.

@LordSimal LordSimal force-pushed the fix-modified-nullable branch from e480d97 to c270ae4 Compare March 6, 2026 13:15
@LordSimal LordSimal changed the base branch from 5.x to 6.x March 6, 2026 13:16
@LordSimal
Copy link
Contributor Author

LordSimal commented Mar 6, 2026

I have adjusted the base to be 6.x
This will go into a new major to let people more easily adjust to the new 5.x migrations major version first.

About schema inconsistencies - we already have that and your tools plugin helps with getting it in sync.
For apps intentionally leaving modified as null - they can just manually call the addColumn method with the desired options.

Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to do in a major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants