From 3362af39963ee3c93aa12f9a3cc2aaa77c04f98e Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 20 Feb 2026 18:35:43 +0100 Subject: [PATCH] Fix column collation not applied for uuid type The MysqlSchemaDialect in cakephp/database only adds COLLATE for text, char, and string types. Since uuid columns generate CHAR(36) but use the 'uuid' type identifier, collation was being ignored. This fix converts uuid to char(36) in mapColumnData() so the dialect properly handles the collation. Fixes the issue reported in #1020. --- src/Db/Adapter/MysqlAdapter.php | 7 ++++++ .../TestCase/Db/Adapter/MysqlAdapterTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index bdbdafeb..fbb553ed 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -274,6 +274,13 @@ public function createTable(Table $table, array $columns = [], array $indexes = */ protected function mapColumnData(array $data): array { + // Convert uuid to char(36) to ensure collation support. + // The dialect only adds COLLATE for text, char, and string types. + if ($data['type'] === self::PHINX_TYPE_UUID) { + $data['type'] = 'char'; + $data['length'] = 36; + } + if ($data['type'] == self::PHINX_TYPE_TEXT && $data['length'] !== null) { $data['length'] = match ($data['length']) { self::TEXT_LONG => TableSchema::LENGTH_LONG, diff --git a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php index f0d3a3bc..1a8e59e5 100644 --- a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php +++ b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php @@ -166,6 +166,29 @@ public function testCreateTable() $this->assertFalse($columns[0]->isSigned()); } + public function testCreateTableWithColumnCollation() + { + $table = new Table\Table('custom_collation', ['id' => false, 'primary_key' => ['id'], 'collation' => 'utf8mb4_unicode_ci']); + + $columnId = new Column(); + $columnId + ->setName('id') + ->setType('uuid') + ->setCollation('ascii_general_ci') + ->setEncoding('ascii'); + $columnName = new Column(); + $columnName + ->setName('name') + ->setType('text'); + $columns = [$columnId, $columnName]; + + $this->adapter->createTable($table, $columns); + + $rows = $this->adapter->fetchAll('SHOW FULL COLUMNS FROM custom_collation'); + $this->assertEquals('ascii_general_ci', $rows[0]['Collation']); // specified collation is set + $this->assertEquals('utf8mb4_unicode_ci', $rows[1]['Collation']); // if not specified uses table's collation + } + public function testCreateTableWithComment() { $tableComment = 'Table comment';