Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shiruken's avatar

Model not inserting timestamp when using PostgreSQL

Hey all!

I recently started trying to port over to Postgres from MySQL, and have bumped into a rather perplexing issue: My models aren't filling in timestamps.

Here's my relevant migration:

        Schema::create('categories', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name')->unique();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('category_nouns', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('noun');
            $table->integer('weight');
            $table->integer('category_id')->unsigned()->default(0);
            $table->foreign('category_id')->references('id')->on('categories');
            $table->timestamps();
            $table->softDeletes();
        });

And here are my model setups:

class Category extends Model {

    use SoftDeletes;

    protected $timestamps = true;
    protected $guarded = ['id'];
    protected $table ='categories';
    
    /* input requirements */
    public static $rules = ['name'=>'required'];

class CategoryNouns extends Model {

    use SoftDeletes;

    protected $timestamps = true;
    protected $guarded = ['id'];
    protected $table ='category_nouns';

    /* input requirements */
    public static $rules = [
                            'noun'        => 'required|unique:category_nouns',
                            'weight'      => 'required|integer',
                            'category_id' => 'required|integer'
                           ];

(the create methods have not been overwritten)

Here's the code:

        // load file
        $inputCsv = Reader::createFromPath($file);

        //@TODO: Progress Bars @ http://laravel.com/docs/5.1/artisan#writing-output

        // assume headers are categories
        $headers = $inputCsv->fetchOne(0);
        foreach($headers as $category) {
            $cat = Category::firstOrCreate(['name'=>$category]);
            $cats[$category] = $cat->id;
        }

        // load nouns and associate to their respective categories
        $data = $inputCsv->setOffset(1)->fetchAssoc(array_flip($cats)); // skip headers now
        foreach($data as $row) {
            foreach($row as $cat_name=>$noun) {
                $noun_exists = CategoryNouns::where('noun','=',$noun)->first();

                if(!empty($noun) && !isset($noun_exists->exists)) {
                    $cat_noun = CategoryNouns::create(['noun' => $noun, 'weight'=> 1, 'category_id' => $cats[$cat_name]]);
                }
            }
        }
        $this->comment("Done.");

And finally, here's the error I keep getting:

[Illuminate\Database\QueryException]                                                                                                                                    
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "created_at" violates not-null constraint                                                           
  DETAIL:  Failing row contains (2, Utilities, null, null, null). (SQL: insert into "categories" ("name") values (Utilities) returning "id")

Any help would be appreciated. Thanks in advance!

0 likes
12 replies
mstnorris's avatar

@shiruken Timestamps are turned on by default so you shouldn't need to explicitly turn them, but in any case the property needs to be public.

public $timestamps = true;
shiruken's avatar

@mstnorris Ironically, I had just changed that from public, to try and get the thing to work. I'll change it back and post back any positive results.

mstnorris's avatar

@shiruken then why don't you just manually insert them?

foreach($headers as $category) {
    $cat = Category::firstOrCreate([
        'name' => $category,
        'created_at' => Carbon::now(), // remember to import Carbon
        'updated_at' => Carbon::now()
    ]);
    $cats[$category] = $cat->id;
}
shiruken's avatar

@mstnorris Because on principle, I shouldn't have to... and also, because it's happening across all my code, not just here. My seeder, my controllers, etc.

shiruken's avatar
shiruken
OP
Best Answer
Level 1

So it turns out I was editing the Models from a different branch, stored in a folder named almost exactly like the one I was trying to work with...

Frustration is a helluva blinder.

javedgardezi's avatar

I am having the same issue with the fresh install of Lavavel 5.1 and Postgres.

Migrated users table correctly and created users table seeder. Ran seeder command and got the following error.

[Illuminate\Database\QueryException]                                                                                                                                                                 
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "created_at" violates not-null constraint                                                                                        
  DETAIL:  Failing row contains (1, Javed, javed@aomcms.com, $2y$10$3GKu.mltQrTsh06syuhgKOiGQstGlmt38j9hTpghBE9A5jDWA4yEG, null, null, null). (SQL: insert into "users" ("email", "name", "password")  
   values (javed@aomcms.com, Javed, yGKu.mltQrTsh06syuhgKOiGQstGlmt38j9hTpghBE9A5jDWA4yEG), (jason@aomcms.com, Jason, ywKHUbntjUy8eV9dEOAxlOlieE1JfbYxLFpzJyoXuOaUw5qvgYgcm))  
[PDOException]                                                                                                                               
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "created_at" violates not-null constraint                                
  DETAIL:  Failing row contains (1, Javed, javed@aomcms.com, $2y$10$3GKu.mltQrTsh06syuhgKOiGQstGlmt38j9hTpghBE9A5jDWA4yEG, null, null, null). 
nwaughachukwuma's avatar

Please any solution on this issue yet, cause am having same challenge.

elhellawy's avatar

add createad_at and updated_at to your fillable array

Please or to participate in this conversation.