cakephp / phinx

PHP Database Migrations for Everyone
https://phinx.org
MIT License
4.46k stars 892 forks source link

[DOCS] Are columns nullable by default? #2207

Closed TheGlenn88 closed 7 months ago

TheGlenn88 commented 1 year ago

I've been on a wild goose chase this morning because there was a period of time that my migrations have changed behaviour on allow null by default for columns, twice in 6 months, so I have a long period where my columns are NOT NULL, a 6 month period where they became allow NULL, recently moving back to NOT NULL.

$this->table('user_things')
            ->addColumn('user_uuid', 'uuid')
            ->addColumn('is_thing', 'boolean', ['default' => false])
            ->create();

If we take the above code as an example. According to the documentation, the user_uuid column should allow nulls.

My understanding however has been that to allow null you would have to explicitly write ->addColumn('user_uuid', 'uuid', ['null' => true])

There have been some changes to Phinx to change this behaviour recently, it appears it may have changed a few times in the past too.

The docs need updating because it currently reads:

allow NULL values, defaults to false if identity option is set to true, else defaults to true

which is untrue as of 0.13.4

I'm not sure what this should read, because I don't know if it's as simple as.

allow NULL values, defaults to false

Can someone with a more in depth understanding of how Phinx works update the docs to reflect it's current effect of 'default'.

dereuromark commented 1 year ago

Changelogs/ReleaseNotes and the feature flag topic will probably give some insight: https://github.com/cakephp/phinx/releases

MasterOdin commented 7 months ago

The behavior for null was changed in 0.13.0 which hopefully clarifies behavior:

set column null by default unless identity by @MasterOdin in #1872. Previously columns were created as NOT NULL by default, and to now get that behavior, you will need to explicitly pass 'null' => false in the column options.

Ideally we'd be able to do the simple "always default to false", but need to put in the bit about identity which inverses the default if enabled which is annoying to describe tersely.