0

I have the following create table statement.

CREATE TABLE `test_table` ( 
`id` INT(11) NOT NULL,
`field1` varchar(10),
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);

when I do the following insert, I'll get the erro "Column 'updated_at' cannot be null"

insert into test_table (id, field1, updated_at) values (1234, 'foo', null);

I expected updated_at would just take on the default value in this case.

mysql version 5.7.12

However when I do this in mysql version 5.6, the insert commands works. Is there a change in the versoin from 5.6 to 5.7? The only difference I thought was 5.7 has NO_ZERO_DATE default to true. But I thought that was only for datetime. Is there a configuration change that I need to make?

It's possible to achieve the result by not passing in updated_at but I don't have over insert statement in this case.

1
  • 1
    updatwe your 5.7.12 to 5.7.31 that works perfectly see dbfiddle.uk/… Commented Jul 16, 2020 at 21:33

2 Answers 2

1

I suspect that this is related to SQL Strict mode - but I can't find the exact quote in the documentation that matches your use case.

Bottom line, you should not be expecting the server to use the default when you provide it with an explicit null value.

If you can't remove the column from the insert list for some reason, a possible workaround is to use the default keyword, which makes your intent unambiguous:

insert into test_table (id, field1, updated_at) 
values (1234, 'foo', default);
Sign up to request clarification or add additional context in comments.

Comments

0

This error is thrown because explicit_defaults_for_timestamp is enabled. Disabling this will solve the issue Refer: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.