4
votes

I am learning laravel 5.1 through laracast fundamentals series and in episode 7 that talk about migrations, i built a table with migration like this:

public function up()
{
    Schema::create('articles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->timestamps();
        $table->timestamp('published_at');
    });
}

and as it says in the episode 10, when i add an article to database through a form that don't have a field for published_at timestamp, then this should not work and should through an exception that Not Null constraint failed But incredibly the article added to table. When i look at the table in phpmyadmin structure for the published_at column it didn't checked the null checkbox, so it shouldn't accept null? is this true?
But then i noticed that the migration sets a default value to 0000-00-00 00:00:00 for published_at column. why?! and will this override the NULL condition? Then i tried to set the default value to none and still the null is not checked. and again i tried to add an article without published_at fields But again a new row added to table with published_at value set to 0000-00-00 00:00:00. any explanation?


I am using wampserver with mysql v5.6.17 , php v5.5.12

2

2 Answers

3
votes

All database types have default values. When you create record and fill only title, then body will be set to empty string, and timestamps to "empty" date what is exactly 0000-00-00 00:00:00. If you made those fields nullable and set ->default(null) for them you will have null values for them in this case.

You can also read about Default values for types in MySql

1
votes

SELECT @@SQL_MODE;.

If you don't see one of the Strict SQL Modes enabled, then MySQL is using its older behavior, and coercing invalid values into something valid to allow the row to be inserted.

If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings.

Default configuration files shipped with official MySQL Server 5.6 had STRICT_TRANS_TABLES enabled. WAMP may not have followed that same convention, perhaps for backward-compatibility reasons... but the configuration can be changed.