简体   繁体   中英

MYSQL Timestamp field inserting NULL values

I have a table with the below column

  `update_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

I have two databases set up using replication. On master while inserting null to the above column, Current time stamp is inserted.

But the same record on being replicated to the slave gives error. Error is that Null value is not permitted for the column

The only difference is that in slave DB, the specific column is indexed in the table.

Mysql reference has the below line

By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values, and assigning NULL assigns the current timestamp.

So i expect the column to get the current time stamp

The data is put to master through spring.

Is this an expected behaviour?

tl;dr

The NOT NULL is permitting you to pass a null value which would normally become current_timestamp .

Original

Its inserted as "null". INSERT INTO table Values(null);

That's forbidden due to the NOT NULL requirement (the point with NOT NULL is to forbid null values) -- you're not suppose to mention the value. Skip the value and the default value will take it's correct place.

Let's take a look at the docs :

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 .

You are permitting null values because of your NOT NULL requirement. In order for your argument to be valid then you need to remove it.

Found the issue. The flag

explicit_defaults_for_timestamp

was not set in replication. Setting this solves the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM