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

biniyam20's avatar

Default value of null for integer turns to 0 during seeding

Hi,

I ran across an issue where I was trying to change my default value of 0 to null for my price field.

        //before
            $table->smallInteger('price')->unsigned()->nullabe()->default(0);
       //after
            $table->smallInteger('price')->unsigned()->nullabe()->default(NULL);

However, I kept running into errors during seeding.

SQLSTATE[HY000]: General error: 1364 Field 'price' doesn't have a default value (SQL: insert into `listings` (`title`, `author`, `isbn`, `isbn13`, `condition`, `additional_information`, `user_id`, `availability`, `upload_date`) values (Dolores est., Arnoldo Haag PhD, 4411084406, 9795024988144, 4, At velit., 1, Y, 2018-05-21 04:52:22))

I found online a solution to mark strict=> for mysql mode in my config/database file and that fixed my error and allowed me to continue however, the default value during my seeding continued to default to 0 even though I ran the migrations again and seeded.

Does anyone know if it is not a good practice to try and set null default values for integer fields because it works perfectly for me to set default null values for my varchar fields? Any guidance on how to set the integer field to null for default would be appreciated. It's not an absolute requirement for my spec but I think it makes more sense if a listing is not for sale, for it have a null price as opposed to have a $0 price.

Thanks.

0 likes
3 replies
realrandyallen's avatar
Level 44

Change nullabe() to nullable() and try it again :) you shouldn't need default(null) - if the field is nullable it will just be null if nothing is passed to it

biniyam20's avatar

@realrandyallen thank you so much... I'm so embarrassed haha. Clearly I had been looking at the code too long to spot that mistake. And thanks for the tip on not needing to use a default value if the field is nullable!

1 like

Please or to participate in this conversation.