'Default value in sql laravel table

I not sure if this is related to Laravel or not but I created the table with Laravel. I've got a table called programmers

DESC programmers;
+--------------+---------------------+------+-----+---------+----------------+
| Field        | Type                | Null | Key | Default | Extra          |
+--------------+---------------------+------+-----+---------+----------------+
| id           | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name         | varchar(255)        | NO   |     | NULL    |                |
| age          | int(11)             | NO   |     | NULL    |                |
| created_at   | timestamp           | YES  |     | NULL    |                |
| updated_at   | timestamp           | YES  |     | NULL    |                |
| framework_id | int(10) unsigned    | NO   |     | NULL    |                |
| test         | tinyint(1)          | NO   |     | NULL    |                |
+--------------+---------------------+------+-----+---------+----------------+

as you can see there's a column called test that's not nullable and has a default value of null. When I to run the following command from the database I expected an error

INSERT INTO  programmers (name, age, framework_id) VALUES ('Melly2', 19, 2)

it actually worked fine and here's the data

SELECT * FROM programmers;
+----+--------+-----+---------------------+---------------------+--------------+------+
| id | name   | age | created_at          | updated_at          | framework_id | test |
+----+--------+-----+---------------------+---------------------+--------------+------+
|  1 | melly  |  20 | 2022-05-03 16:36:12 | 2022-05-03 16:36:12 |            1 |    0 |
|  2 | Melly2 |  19 | NULL                | NULL                |            2 |    0 |
+----+--------+-----+---------------------+---------------------+--------------+------+

the test column actually defaulted to 0 not null, and if I were to run the following command it tells me I can't have null as a value as expected

INSERT INTO  programmers (name, age, framework_id, test) VALUES ('Melly2', 19, 3, null);
ERROR 1048 (23000): Column 'test' cannot be null

question: can someone briefly explain why test column didn't default to null?



Solution 1:[1]

In this scenario, the default value is null only if you don't provide a value. But when you provide some value, it should be compatible with the datatype you set for the column.

Here the datatype is tinyint. So, you should provide the values from true/false which infact will be converted into 1/0; else you should insert integers example:0,1,2,... etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S K R