4

While trying to insert a record into a certain table, I'm getting an error as follows:

Data truncation: Incorrect datetime value: '0000-00-00 00:00:00' for column 'deleted_at' at row 1

Searching around I found the following questions to be quite insightful:

But I can't take any of the solutions given in these answers because of the following reasons:

  • Using any other JAR as mentioned here and here (although this isn't a maven project) isn't an option. I have to use the same jar.
  • I can't use anything other than '0000-00-00 00:00:00' for the field deleted_at. Apparently, there is a buttload of checking against this. You see, initially, delete_at date can't be set (I would have used a flag like is_deleted. If there is a better way please tell me).
  • And changing MySQL configuration isn't an option like mentioned here. He did a nice job of explaining it. I believe this is the main issue.

As to why you are encountering the issue, it's likely that the sql_mode setting for your session includes NO_ZERO_DATES.

I (sadly) don't have much control over these things as I'm not the only one working on this project. To give you an insight about the project, this is how I'm getting the error.

enter image description here

They don't use any logging. Hope you get the picture.

So, given the constraints I'm bound with, is there a way to insert '0000-00-00 00:00:00' other than the solutions given above (I'm hoping maybe using something in JDBC).

MySQL Server Version: 5.7.12-0ubuntu1

Community
  • 1
  • 1
UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
  • What about using null instead of '0000-00-00 00:00:00'? Could this be possible? – Christian Klemm Apr 30 '16 at 19:26
  • @chris579 Had thought about it but these guys have done a string comparison to check if a record is deleted or not. If they were willing to change the code or the configuration, there wouldn't have been as issue. – UrsinusTheStrong Apr 30 '16 at 19:39
  • 2
    Well you're talking about "these guys", what are they about? Would be helpful to know what we're talking about. To be honest I'm quite sure that there is no way around. It seems that you are extending the code of somebody else. If so it would be quite harsh to find a proper workaround – Christian Klemm Apr 30 '16 at 19:42
  • 2
    I agree with chris579, it seems that the server configuration do not allow that value. If you can not change the configuration and you must insert that value, you're in a deadlock. How do "these guys" insert new records? – RubioRic Apr 30 '16 at 19:48
  • @chris579 By these guys I mean the other employees (including my lead) employed by my current employer. Yes, I'm working on someone else's code (a whole project actually). I suspected that there isn't any workaround but still wasted almost two days on it. When I told my lead about the issues, he said that I didn't want to find a solution and walked of. – UrsinusTheStrong Apr 30 '16 at 19:48
  • @RubioRic I don't know what happened down the line. The client had complained about some issue with records not getting deleted and another developer (also my senior) had patched the code. Then I was assigned with making a new module with some interfaces. Started facing this issue. I think the insert worked earlier. – UrsinusTheStrong Apr 30 '16 at 19:52
  • 1
    @UrsinusTheStrong Wow, props to your lead. Seems to be really empathetic. I hope for you that you earn money for that work otherwise I would leave this really fast. Btw still wondering about a company/project working with gui and java. Switched because of this to .net – Christian Klemm Apr 30 '16 at 19:52
  • @chris579 Earlier they were using Swing. Shifted to JavaFx about 2 years ago for the new version of an earlier app. Has become a never ending project since then. I'm already looking for a switch. – UrsinusTheStrong Apr 30 '16 at 19:55

1 Answers1

6

After three days of toil, I finally worked out a solution.

As mentioned in this answer, MySQL won't allow Zero Dates to be inserted if the variable sql_mode is set with NO_ZERO_DATES.

As to why you are encountering the issue, it's likely that the sql_mode setting for your session includes NO_ZERO_DATES.

Now, I could have just cleared the variable sql_mode by doing the following in the command line:

SET GLOBAL sql_mode = '';

But as mentioned before, I'm not allowed to do this. So, I needed a session based solution. Which was given in this document itself.

SET SESSION sql_mode = '';

So, I added the following lines after opening the connection.

try (Statement statementSet = connection.createStatement()) {
    statementSet.execute("SET SESSION sql_mode = ''");
}

And it worked. I was able to insert and update datetime fields with '0000-00-00 00:00:00'. This cleared the sql_mode variable only for the current session. The global settings are as it is.

mysql> SHOW VARIABLES LIKE 'sql_mode';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                                     |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

I had to do this because I had no other option. This isn't recommended.

Community
  • 1
  • 1
UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33