From 65fa367570969fb3961fd50dd9bafb9cd01e89c6 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 13 Jun 2017 23:17:36 -0400 Subject: [PATCH] Don't add auto increment to columns that turn it off. When autoIncrement has been set to a specific value we shouldn't overwrite the user's request. This allows integer keys to exist without autoincrement being set. Refs #10765 Refs #10045 --- src/Database/Schema/MysqlSchema.php | 3 ++- tests/TestCase/Database/Schema/MysqlSchemaTest.php | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Database/Schema/MysqlSchema.php b/src/Database/Schema/MysqlSchema.php index fb161a0aa3d..28d4d727c45 100644 --- a/src/Database/Schema/MysqlSchema.php +++ b/src/Database/Schema/MysqlSchema.php @@ -382,7 +382,8 @@ public function columnSql(TableSchema $schema, $name) } $addAutoIncrement = ( [$name] == (array)$schema->primaryKey() && - !$schema->hasAutoincrement() + !$schema->hasAutoincrement() && + !isset($data['autoIncrement']) ); if (in_array($data['type'], ['integer', 'biginteger']) && ($data['autoIncrement'] === true || $addAutoIncrement) diff --git a/tests/TestCase/Database/Schema/MysqlSchemaTest.php b/tests/TestCase/Database/Schema/MysqlSchemaTest.php index 1bbdf2f6e70..b6e2e9304ea 100644 --- a/tests/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/tests/TestCase/Database/Schema/MysqlSchemaTest.php @@ -592,6 +592,11 @@ public static function columnSqlProvider() ['type' => 'integer', 'length' => 20, 'autoIncrement' => true], '`post_id` INTEGER(20) AUTO_INCREMENT' ], + [ + 'post_id', + ['type' => 'integer', 'length' => 20, 'null' => false, 'autoIncrement' => false], + '`post_id` INTEGER(20) NOT NULL' + ], [ 'post_id', ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],