We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[BUG] INSERT INTO table (col1,col3) VALUES (x,y) never works if col2 is a NOT NULL column.

It took me a lot to discover but there is it is. Totally proved. I'm using MariaDB, and phalcon 2.0 x64 VC11. I stuck with it when trying to insert a new row in tableB inside a model of tableA. ID of tableB(child) is a foreign key to ID of table A(father). If there's any NOT NULL with a default value column in the table it will just do nothing(no error, no warning...but no insert as well). It happends with phql and also with ORM.



5.7k
edited Apr '15

Have you tried setting the default value in the model like so :

    public function beforeValidationOnCreate()
    {
        if ( !isset($this->field) || empty($this->field) ) {
            $this->field = new \Phalcon\Db\RawValue('default');
        }
    }

I've tried and worked just once, then left my database in this state: Error: Failed to read auto-increment value from storage engine

I needed droping and reassign the autoincrement type. Then it didnt work anymore but like at the beginning, with the null values.

Have you tried setting the default value in the model like so :

   public function beforeValidationCreate()
   {
       if ( !isset($this->field) || empty($this->field) ) {
           $this->field = new \Phalcon\Db\RawValue('default');
       }
   }


5.7k

Have you tried changing beforeValidationOnCreate() to beforeValidation() ?

Doesn't. And on the way I've found two more bugs on the querybuilder of phalcon...really is getting a pain in the ass, I waste more time finding bugs than programming.

Have you tried changing beforeValidationOnCreate() to beforeValidation() ?

Hi, i had similiar problem with phalcon 1.3 ( or 1.2?) and oracle sql. Now i always set default values to model definition like this:

class ClassName extends \Phalcon\Mvc\Model {

    /**
     * ID
     *
     * @var int
     * @access protected
     */
    protected $ID;

    /**
     * SOME_COLUMN
     *
     * @var int
     * @access protected
     */
    protected $SOME_COLUMN = 'default_value';

    ...
}

I hope it helps or inspire you...