Bug #52014

Migration makes fields NOT NULL even though not true

Added by Soren Malling almost 2 years ago. Updated almost 2 years ago.

Status:New Start date:2013-09-15
Priority:Should have Due date:
Assigned To:- % Done:

0%

Category:Persistence
Target version:-
PHP Version: Complexity:
Has patch:No Affected Flow version:Git master

Description

Hi,

Shitty description, so I hope this example explains it further:

Running latest composer update

Model


/**
 * @Flow\Entity
 */
class Product {

    /**
     * Title
     *
     * @var string
     * @Flow\Validate(type="NotEmpty")
     */
    protected $title;

    /**
     * Description
     *
     * @var string
     */
    protected $description;

    /**
     * Price
     *
     * @var boolea
     */
    protected $price;

Migration:

$this->addSql("CREATE TABLE vendor_project_domain_model_product (persistence_object_identifier VARCHAR(40) NOT NULL, title VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, price DOUBLE PRECISION NOT NULL, PRIMARY KEY(persistence_object_identifier)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");

Issue:

Submitting a form with the "title" and "description" field, only filling out the "title" field throws the following error

An exception occurred while executing 'INSERT INTO vendor_project_domain_model_product (persistence_object_identifier, title, description, price) VALUES (?, ?, ?, ?)' with params ["f758bfb0-8270-4e13-f2b0-606739355187", "Produkt navn", "", null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'price' cannot be null 

Price field is required (on database (See migration) level) even though not marked as validate(type="NotEmpty") in my model.

Migration generated with ./flow doctrine:migrationgenerate

History

#1 Updated by Philipp Maier almost 2 years ago

Although a little bit of a late answer:

In general, validation has no influence on the database schema. By default, Doctrine expects all of the objects properties to be filled with some value other than NULL. Because of this, all database fields are configured to NOT NULL.

There are several ways to deal with this:
1) You define a standard value. This can be done with the constructor or in this manner:

    /**
     * Price
     *
     * @var boolean
     */
    protected $price = FALSE;

2) You tell Doctrine to explicitly allow NULL values for this property:

    /**
     * Price
     *
     * @var boolean
     * @ORM\Column(nullable=true)
     */
    protected $price;

Either way, this is intended behaviour and not a bug.
Hope it helped :)

Also available in: Atom PDF