"Syntax error or access violation" - 从 Schema::facade 迁移,但语句执行无误
"Syntax error or access violation" - in migration from Schema:: facade, but statement executes without error
我写了一个迁移,它在我的测试数据库上执行得很好——做了几十个测试 运行s 没有问题。我 运行 它在我的产品数据库的克隆上,突然间我遇到了各种各样的问题。我开始认为这是数据库配置或权限问题,但我以 root 身份登录到此克隆,所以我什至不确定从哪里开始寻找...
如果我从错误中复制 mysql 语句(...并修复丢失的数据),该语句将毫无问题地执行。
ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE original_inventory initial_inventory INT DEFAULT NULL;
违规行:
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
错误:
[PDOException (42000)]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-tmp_09-10-2020 CHANGE original_inventory initial_inventory INT DEFAULT NULL' at line 1
迁移:
public function up()
{
/* 1. Backup Existing Tables */
DB::statement('CREATE TABLE `retail_items-tmp_09-10-2020` LIKE `retail_items`; ');
DB::statement('CREATE TABLE `ARCH__retail_items_09-10-2020` LIKE `retail_items`; ');
DB::statement('INSERT INTO `retail_items-tmp_09-10-2020` SELECT * FROM `retail_items`; ');
DB::statement('INSERT INTO `ARCH__retail_items_09-10-2020` SELECT * FROM `retail_items`;');
/* 2. Update structure */
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->integer('event_ID')->length(11)->unsigned()->nullable();
$table->foreign('event_ID')->references('event_ID')->on('events');
});
/* 3. Update structure that would have been destructive prior to step 3 */
// When I had this piece of code included, it resulted in the same error "Syntax error or access violation..." this worked in testing, but throws errors on Prod, changed to DB:statement below with success.
// Schema::table('retail_items-tmp_09-10-2020', function($table) {
// $table->smallInteger('flag')->unsigned()->nullable(false)->default(0)->change();
// });
$query = "ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE `flag` `flag` SMALLINT(11) DEFAULT 0 NOT NULL; ";
DB::statement($query);
}
我已经坚持了一段时间,所以任何理论、寻找的地方等都将不胜感激。
(我有另一个重命名临时 table 的迁移。我有一些迁移和数据操作,用我编写的这段代码总共需要 10 分钟以上的时间来执行我正在启动它,所以温度 tables 是必要的,以防止在生产中启动时停机)
在这个问题上我仍然有更多的故障排除/其他障碍,所以这只删除了一个...
即使引用得当,我还是阅读了很多 S.O。关于这个主题的帖子,正如 Webtect 所建议的那样,我最终只是简单地删除了 all 破折号,并且事情更加一致和可靠。我仍然觉得 为什么 是个谜,因为我以前在这样的日期中使用过连字符 - 正确引用 - 没有问题。但这确实让我大吃一惊。我想我将完全停止在 dated-table-names 中使用连字符的做法。它们似乎比改进的可读性保证更令人头疼。
我写了一个迁移,它在我的测试数据库上执行得很好——做了几十个测试 运行s 没有问题。我 运行 它在我的产品数据库的克隆上,突然间我遇到了各种各样的问题。我开始认为这是数据库配置或权限问题,但我以 root 身份登录到此克隆,所以我什至不确定从哪里开始寻找...
如果我从错误中复制 mysql 语句(...并修复丢失的数据),该语句将毫无问题地执行。
ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE original_inventory initial_inventory INT DEFAULT NULL;
违规行:
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
错误:
[PDOException (42000)]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-tmp_09-10-2020 CHANGE original_inventory initial_inventory INT DEFAULT NULL' at line 1
迁移:
public function up()
{
/* 1. Backup Existing Tables */
DB::statement('CREATE TABLE `retail_items-tmp_09-10-2020` LIKE `retail_items`; ');
DB::statement('CREATE TABLE `ARCH__retail_items_09-10-2020` LIKE `retail_items`; ');
DB::statement('INSERT INTO `retail_items-tmp_09-10-2020` SELECT * FROM `retail_items`; ');
DB::statement('INSERT INTO `ARCH__retail_items_09-10-2020` SELECT * FROM `retail_items`;');
/* 2. Update structure */
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->integer('event_ID')->length(11)->unsigned()->nullable();
$table->foreign('event_ID')->references('event_ID')->on('events');
});
/* 3. Update structure that would have been destructive prior to step 3 */
// When I had this piece of code included, it resulted in the same error "Syntax error or access violation..." this worked in testing, but throws errors on Prod, changed to DB:statement below with success.
// Schema::table('retail_items-tmp_09-10-2020', function($table) {
// $table->smallInteger('flag')->unsigned()->nullable(false)->default(0)->change();
// });
$query = "ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE `flag` `flag` SMALLINT(11) DEFAULT 0 NOT NULL; ";
DB::statement($query);
}
我已经坚持了一段时间,所以任何理论、寻找的地方等都将不胜感激。
(我有另一个重命名临时 table 的迁移。我有一些迁移和数据操作,用我编写的这段代码总共需要 10 分钟以上的时间来执行我正在启动它,所以温度 tables 是必要的,以防止在生产中启动时停机)
在这个问题上我仍然有更多的故障排除/其他障碍,所以这只删除了一个...
即使引用得当,我还是阅读了很多 S.O。关于这个主题的帖子,正如 Webtect 所建议的那样,我最终只是简单地删除了 all 破折号,并且事情更加一致和可靠。我仍然觉得 为什么 是个谜,因为我以前在这样的日期中使用过连字符 - 正确引用 - 没有问题。但这确实让我大吃一惊。我想我将完全停止在 dated-table-names 中使用连字符的做法。它们似乎比改进的可读性保证更令人头疼。