I have encountered a situation when doing a database migration that contradicted what I thought I knew about php connections to a database. I wanted to change the collation of a column, so I wrote the following statement:
ALTER TABLE posts MODIFY COLUMN text_html LONGTEXT COLLATE utf8_unicode_ci
An this works correctly using mysql cli. Now, in order to have this change tracked in code, I created a Laravel migration, with the following statement:
DB::unprepared('ALTER TABLE posts MODIFY COLUMN text_html LONGTEXT COLLATE utf8_unicode_ci');
and I got the following error:
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'published_at' (SQL: ALTER TABLE posts MODIFY COLUMN text_html LONGTEXT COLLATE utf8_unicode_ci)
I thought it may have something to do with Laravel, so I tried doing a more "raw" query:
DB::connection()
->getDoctrineConnection()
->exec('ALTER TABLE posts MODIFY COLUMN text_html LONGTEXT COLLATE utf8_unicode_ci');
Which gave me a similar error:
An exception occurred while executing 'ALTER TABLE posts MODIFY COLUMN text_html LONGTEXT COLLATE utf8_unicode_ci': SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'published_at'
So there are actually two problems here. The first one, obviously, why am I getting an error about the published_at column if I'm trying to modify the text_html column. But the second one, the one I'm more interested in, is why it works on mysql cli and it doesn't with php. If running raw queries in php is not the same as running queries with mysql cli, this tells me php is doing something more than just running the query. What is it doing?