josegonzalez / cakephp-version

CakePHP3: plugin that facilitates versioned database entities
MIT License
51 stars 22 forks source link

Should "content" field be nullable? #18

Closed garethellis36 closed 7 years ago

garethellis36 commented 7 years ago

CREATE TABLEversion( idint(11) NOT NULL AUTO_INCREMENT, version_idint(11) DEFAULT NULL, modelvarchar(255) NOT NULL, foreign_keyint(10) NOT NULL, fieldvarchar(255) NOT NULL, contenttext, createddatetime NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The NULLability of the 'content' field is not specified in this SQL. We are getting save failures for new entities with the following error:

Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'content' cannot be null

We used the following migration:

<?php

use Phinx\Migration\AbstractMigration;

class CreateVersions extends AbstractMigration
{
    public function change()
    {
        //set-up according to https://github.com/josegonzalez/cakephp-version
        $this->table('version')
             ->addColumn('version_id', 'integer', ['null' => true])
             ->addColumn('model', 'string')
             ->addColumn('foreign_key', 'integer')
             ->addColumn('field', 'string')
             ->addColumn('content', 'text')
             ->addColumn('created', 'datetime')
             ->addColumn('user_id', 'integer', ['null' => true])
             ->create();
    }
}

Do we need to make the content field nullable?

josegonzalez commented 7 years ago

Yeah, if one of the fields in your model is nullable, then that content field will also need to be nullable.