-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timestamp behavior on MariaDB is strange but MySQL is fine #924
Comments
Another workaround I have used is to set the default values manually: table.timestamp('created_at', { useTz: true })
.notNullable()
.defaultTo(this.raw('CURRENT_TIMESTAMP'));
table.timestamp('updated_at', { useTz: true })
.notNullable()
.defaultTo(this.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')); That way the columns do not have to be nullable. |
@aa-ndrej many ORMs allow your update a model's mics attributes without updating timestamps. I am afraid table.timestamp('created_at', { useTz: true })
.notNullable()
.defaultTo(this.raw('CURRENT_TIMESTAMP'));
table.timestamp('updated_at', { useTz: true })
.notNullable()
.defaultTo(this.raw('CURRENT_TIMESTAMP')); |
i have the same problem here. Was the problem not detected by the maintainers? It has been reported for some time and the topic remains unanswered |
With database debugging enabled, it can be noticed after multiple tries that Lucid always sets created_at field as null during records updates, which conflicts with the very way by which MariaDB handles null values passed for TIMESTAMP fields as clearly stated here by always setting the field value to the current timestamp I conducted the tests using Lucid v18.4.0 and MariaDB 10 This is an actual database debug output following an update statement using MariaDB:
I tried to pass the created_at field its original value using SQL client (without Lucid) and it behaves sanely, whereas, not passing the field a value at all will be considered as passing it a null value |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Could you please add a proper test and setup a |
I am not sure what exactly needs to be done here. If I read the original post, it mentions MariaDB self deciding to stick an If my assumption is right, then there isn't much we could do apart from mentioning it explicitly inside the docs with this workaround. #924 (comment) |
Package version
18.3.0
Node.js and npm version
18.15.0
9.5.0
Sample Code (to reproduce the issue)
Make a very simple model with created_at and updated_at:
And table migration:
In controller, I update an existing model:
In MySQL,
created_at
stays the same whileupdated_at
get updated. Work as expected.But in MariaDB, both
created_at
andupdated_at
got updated, which is unexpected.Table scheme in MariaDB:
created_at
is clearly wrong.I checked MariaDB's doc and it seems by design that MariaDB will make first not null timestamp column auto-update:
Well, in MySQL doc, you have to manually specify default and auto update... (if i understand it correctly)
In short, MySQL and MariaDB treat your first
TIMESTAMP NOT NULL
column differently:A workaround can be, force the column to be nullable:
If you don't like nullable, you can set a raw default:
The text was updated successfully, but these errors were encountered: