Bug #76508
closedComparison of NOT NULL fields without a default value does not work in "Compare Database" tool if a field is not autoincrement
100%
Description
In some cases you would like to have a field which should never be NULL, eg. a referencing ID to parent object. But if a database field is defined as NOT NULL field without a default value the comparison does not work because the tool expects NULL as default value. A default of '0' would work but allows inserting of rows without the specific field explicit set.
For example the SQL definition looks like that:
CREATE TABLE tx_foo ( uid int(11) unsigned NOT NULL auto_increment, pid int(11) unsigned DEFAULT '0' NOT NULL, tstamp int(11) unsigned DEFAULT '0' NOT NULL, crdate int(11) unsigned DEFAULT '0' NOT NULL, someid int(11) unsigned NOT NULL, PRIMARY KEY (uid), KEY parent (pid), KEY someid (someid) ) ENGINE=InnoDB;
The install tool will always show a difference for the "someid" field and expects:
someid int(11) unsigned DEFAULT NULL NOT NULL,
But this would result in invalid SQL when creating the table.
So the SqlSchemaMigrationService should be modified to prevent this edge case:
LINE 517 in SqlSchemaMigrationService.php (assembleFieldDefinition method)
// Add a default value if the field is not auto-incremented (these fields never have a default definition) // but not if the field can not be NULL and default value is NULL (this results in invalid SQL) if (!stristr($row['Extra'], 'auto_increment') && !($row['Null'] == 'NO' && $row['Default'] === NULL)) {
Updated by Morton Jonuschat almost 9 years ago
- Status changed from New to Needs Feedback
someid int(11) unsigned DEFAULT NULL NOT NULL,is the definition reported by MySQL for that field (the MySQL server adds the implicit default value), so if you add the full definition of the column to your ext_tables.sql file everything should be ok.
Updated by M. Stichweh almost 9 years ago
Morton Jonuschat wrote:
[...] is the definition reported by MySQL for that field (the MySQL server adds the implicit default value), so if you add the full definition of the column to your ext_tables.sql file everything should be ok.
This is correct, if I define the "someid" field in this way in my ext_tables.sql, the compare tool works properly. But when the table is created the first time I get a MySQL (MariaDB) error:
#1067 - Invalid default value for 'someid'
Because of that I cannot define the field like that and the compare tool will always show a difference. This is a problem when updating Typo3 for example since the database update step of the migration wizard will never succeed.
Updated by Gerrit Code Review over 8 years ago
- Status changed from Needs Feedback to Under Review
Patch set 12 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/49593
Updated by Gerrit Code Review over 8 years ago
Patch set 13 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/49593
Updated by Gerrit Code Review over 8 years ago
Patch set 14 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/49593
Updated by Gerrit Code Review over 8 years ago
Patch set 15 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/49593
Updated by Morton Jonuschat over 8 years ago
- Status changed from Under Review to Resolved
- % Done changed from 0 to 100
Applied in changeset 2ba08444c9cd7613c97e10b65fd53867565f205c.