Topic: Migrating to MySQL default value for Date not null?

Hi all,

I am trying to migrate an application that always was working with Sqlite which contains 2 Date columns that should not be null. While it was working fine with Sqlite which stores dates as text, the creation of these columns is problematic when connecting to a blank MySql database when Myvisualdatabase tries to create the tables. It trows the infamous "Invalid default value for 'Date_Column'.

I have tried to change the default value with "CURRENT_DATE" or "CURRENT_TIMESTAMP" in MyVisualDatabase with no luck, I still get the same error when creating the tables in MySQL. Any experience on anyone side?

Re: Migrating to MySQL default value for Date not null?

tcoton wrote:

Hi all,

I am trying to migrate an application that always was working with Sqlite which contains 2 Date columns that should not be null. While it was working fine with Sqlite which stores dates as text, the creation of these columns is problematic when connecting to a blank MySql database when Myvisualdatabase tries to create the tables. It trows the infamous "Invalid default value for 'Date_Column'.

I have tried to change the default value with "CURRENT_DATE" or "CURRENT_TIMESTAMP" in MyVisualDatabase with no luck, I still get the same error when creating the tables in MySQL. Any experience on anyone side?

Have you tried setting the default value to zero (0)? basically datetime in mysql is just a float

brian

Re: Migrating to MySQL default value for Date not null?

Yes, I tried zero and I get the same error saying that it is a wrong default value for a date. However, removing the non null constraint, then modifying the table with the right parameters within MySQL does the trick. It does work also to create the table directly in MySQL before connecting the app, it is just more cumbersome.

Re: Migrating to MySQL default value for Date not null?

tcoton wrote:

Yes, I tried zero and I get the same error saying that it is a wrong default value for a date. However, removing the non null constraint, then modifying the table with the right parameters within MySQL does the trick. It does work also to create the table directly in MySQL before connecting the app, it is just more cumbersome.

Maybe something about mysql version. I'm using 5.6.51.

brian

Re: Migrating to MySQL default value for Date not null?

I am using MariaDB 10.11.2 and I found the default value that works is : "0000-00-00" which makes sense considering that it matches the default date format.

Re: Migrating to MySQL default value for Date not null?

If you create tables in a MYSQL/MARIADB database, you are required to create a connection (name/password and database name) without creating tables in third-party programs. When you start the program and connect to the database, MVD will automatically create tables in your database (subject to all permissions in MYSQL/MARIADB). There are no problems with dates in MYSQL/MARIADB from versions 5.6 to 8 or 10 in MYSQL/MARIADB. Just tested. Brian right

Re: Migrating to MySQL default value for Date not null?

@Sparrow

Yes it does but then, if you set a date column not null, you must set a default value and the default value 'now' that works in SQLite does is not accepted by MysQL/MariaDB, there is an error and the table cannot be created,  hence my answer with the correct format above your comment.

Another hiccup when connecting to a fresh database with no tables in MySQL/MariaDB from a MVD project, it tries to create twice the table "_user", throws a duplicate error and creates a useless table "_mvd_roles". This never happens when connecting to an empty database from a compiled project. I tried it multiples times when testing my Delete - Import routine. You can also, as I mentioned before, connect the application to an already filled up database containing the exact structure required with no problem at all.

The subject of this post was really about the default value of a Date column when not null is forced in MySQL.

Re: Migrating to MySQL default value for Date not null?

Similar errors (“Invalid default value for 'Date_Column’) occur if the table has already been created in the database.
Both for SQLite and MYSQL/MARIADB.
I was unable to reproduce your situation for both DATE and ROLES.
All tables are created without errors if the database is clean.
After creating the tables, an attempt to set the field flag to mandatory results in an error (“Invalid default value ....)
You can attach an example or describe step by step how and what you do to reproduce your situation.

9 (edited by brian.zaballa 2024-03-02 07:01:34)

Re: Migrating to MySQL default value for Date not null?

Well, here's mine, no problem at all.
https://1drv.ms/v/s!AlhiHP7f0ZGDh6Qba-O … w?e=SoEhmh

brian

Re: Migrating to MySQL default value for Date not null?

tcoton wrote:

@Sparrow

Yes it does but then, if you set a date column not null, you must set a default value and the default value 'now' that works in SQLite does is not accepted by MysQL/MariaDB, there is an error and the table cannot be created,  hence my answer with the correct format above your comment.

Another hiccup when connecting to a fresh database with no tables in MySQL/MariaDB from a MVD project, it tries to create twice the table "_user", throws a duplicate error and creates a useless table "_mvd_roles". This never happens when connecting to an empty database from a compiled project. I tried it multiples times when testing my Delete - Import routine. You can also, as I mentioned before, connect the application to an already filled up database containing the exact structure required with no problem at all.

The subject of this post was really about the default value of a Date column when not null is forced in MySQL.

You mention again here the default value being 'now', changing it to zero fixes the problem for me. If possible, maybe you can send here the tables.ini you are working with so that we can test on it.

brian

Re: Migrating to MySQL default value for Date not null?

You are trying to show me that MYSQL does not support 0 or 'now' in date and time. This is known.
But this is not a drawback of MYSQL, but a feature of SQLITE.
For SQLITE, there is no strict correspondence between a given column type and its contents in a cell,
and this is described in the SQLite documents.
As for the default date value, you can assign it in the form in which you define this value.
For example, by writing it in a script.

12 (edited by brian.zaballa 2024-03-02 08:55:29)

Re: Migrating to MySQL default value for Date not null?

sparrow wrote:

You are trying to show me that MYSQL does not support 0 or 'now' in date and time. This is known.
But this is not a drawback of MYSQL, but a feature of SQLITE.
For SQLITE, there is no strict correspondence between a given column type and its contents in a cell,
and this is described in the SQLite documents.
As for the default date value, you can assign it in the form in which you define this value.
For example, by writing it in a script.

Zero (0) was my solution and working for me, check on my video. I'm aware of the capabilities of sqlite and mysql. I'm addressing that video to tcoton.

brian

13 (edited by sparrow 2024-03-02 09:44:17)

Re: Migrating to MySQL default value for Date not null?

Brian, Sorry, I didn't look at the sender)))


I'm using a Required parameter but defining the value for the field directly on the form. The value 0 is not informative for me.

p.s. By the way, MARIADB 10.10.3 does not allow you to create from 0.

Re: Migrating to MySQL default value for Date not null?

sparrow wrote:

Brian, Sorry, I didn't look at the sender)))


I'm using a Required parameter but defining the value for the field directly on the form. The value 0 is not informative for me.

p.s. By the way, MARIADB 10.10.3 does not allow you to create from 0.

What you mean by create from 0? Set default value as zero?
If that's the case then most likely MariaDB Ver10.11.2 tcoton's using also not allowing zero as default value then. Sorry for I'm not so familiar with MariaDB since I'm working with MySQL Community edition ever since and don't have time to test on it as of the moment.
Anyway @tcoton, I think setting it as 0000-00-00 instead is not that tedious. You'll only set this once.

brian

Re: Migrating to MySQL default value for Date not null?

I am now testing with MySQL community edition 8 and it does not accept neither 0 or 0000-00-00 as a default value if null for a date column. According to the documentation, the minimum date should be 1000-01-01.

This is just an information in case of anyone would need it.