Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP can be null on one machine but not another?

Tags:

mysql

I have a MySql table with a field defined as:

`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

On my local machine, I can run:

INSERT INTO mytbl (id, user_id, created) VALUES(88882341234, 765, null);
SELECT id, user_id, created FROM mytbl WHERE id = '88882341234';

And then 'created' will show something like '2014-06-13 21:16:42'.

But on my staging server, if I run the same queries, I get this error:

Column 'created' cannot be null.

The schemas of the tables are the same (across local and staging), which I ensured via mysqldump (to clone the table before running this test).

I'm running MySql 5.6.17 on both machines. I've also ensured that both have the same sql_mode.

What could be the problem?

P.S. For people who don't know why I'd be setting a non-nullable field's value to null, MySql Docs say:

In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.

like image 593
Ryan Avatar asked Jun 13 '14 21:06

Ryan


People also ask

How do I set MySQL default to null timestamp?

To permit a TIMESTAMP column to contain NULL , explicitly declare it with the NULL attribute. In this case, the default value also becomes NULL unless overridden with a DEFAULT clause that specifies a different default value. DEFAULT NULL can be used to explicitly specify NULL as the default value.

Can we use default with not null?

Yes, adding a column with NOT NULL and a default doesn't actually write the values to all the rows at the time of the alter, so it is no longer a size-of-data operation. When you select from the table, the columns are actually materialized from sys.

What is the default value of timestamp in MySQL?

A TIMESTAMP column that permits NULL values does not take on the current timestamp at insert time except under one of the following conditions: Its default value is defined as CURRENT_TIMESTAMP and no value is specified for the column.

What is Current_timestamp in MySQL?

MySQL CURRENT_TIMESTAMP() Function The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).


1 Answers

I found what the problem was. The MySql variable/parameter explicit_defaults_for_timestamp was OFF on my local machine but ON on my remote machine.

I visited my AWS RDS Parameter Groups page and changed explicit_defaults_for_timestamp from 1 to 0. Then I went to my AWS RDS instances page to watch when "Parameter Group" changed from "Applying" to "pending-reboot". Then I rebooted the particular instance.

These links helped me:

  • https://stackoverflow.com/a/23392448/470749
  • How to import MySQL binlog that contains INSERTs of a TIMESTAMP field with default value CURRENT_TIMESTAMP
  • https://forums.aws.amazon.com/thread.jspa?threadID=132676
like image 121
Ryan Avatar answered Oct 18 '22 05:10

Ryan