From bd5200dfb2a3dff0cf58daf9a3a6313a2dd2f89b Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Sun, 5 Jan 2025 10:54:17 -0500 Subject: [PATCH] Fix issue processwire/processwire-issues#1966 --- wire/core/Field.php | 5 ++++- wire/core/Fields.php | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/wire/core/Field.php b/wire/core/Field.php index 5f8978e6..198d53e5 100644 --- a/wire/core/Field.php +++ b/wire/core/Field.php @@ -1248,7 +1248,9 @@ class Field extends WireData implements Saveable, Exportable { $table = $this->setTable; } else { $name = $this->settings['name']; - if(!strlen($name)) throw new WireException("Field 'name' is required"); + $length = strlen($name); + if(!$length) throw new WireException("Field 'name' is required"); + if($length > 58) $name = substr($name, 0, 58); // 'field_' + 58 = 64 max $table = self::tablePrefix . $name; } if(self::$lowercaseTables) $table = strtolower($table); @@ -1265,6 +1267,7 @@ class Field extends WireData implements Saveable, Exportable { */ public function setTable($table = null) { $table = empty($table) ? '' : $this->wire()->sanitizer->fieldName($table); + if(strlen($table) > 64) $table = substr($table, 0, 64); $this->setTable = $table; } diff --git a/wire/core/Fields.php b/wire/core/Fields.php index 823f2dbf..0b5bcad0 100644 --- a/wire/core/Fields.php +++ b/wire/core/Fields.php @@ -388,12 +388,14 @@ class Fields extends WireSaveableItems { // even if only the case has changed. $schema = $item->type->getDatabaseSchema($item); if(!empty($schema)) { - foreach(array($table, "tmp_$table") as $t) { + list(,$tmpTable) = explode(Field::tablePrefix, $table, 2); + $tmpTable = "tempf_$tmpTable"; + foreach(array($table, $tmpTable) as $t) { if(!$database->tableExists($t)) continue; throw new WireException("Cannot rename to '$item->name' because table `$table` already exists"); } - $database->exec("RENAME TABLE `$prevTable` TO `tmp_$table`"); // QA - $database->exec("RENAME TABLE `tmp_$table` TO `$table`"); // QA + $database->exec("RENAME TABLE `$prevTable` TO `$tmpTable`"); // QA + $database->exec("RENAME TABLE `$tmpTable` TO `$table`"); // QA } $item->prevTable = ''; }