gpt4 book ai didi

migration - Laravel,通过迁移创建触发器运行成功,但在 mysql 中找不到触发器

转载 作者:行者123 更新时间:2023-12-05 07:36:08 25 4
gpt4 key购买 nike

我在 laravel 中使用迁移创建了一个触发器并且它迁移成功,但是我在数据库中找不到触发器,有人可以帮忙吗。

我的 Laravel 版本是 5.5

我在下面添加了整个迁移文件。

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTrigger extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$database = config('database.default');
$databaseUserName = config('database.connections.'.$database.'.username');

$definer = 'DEFINER=`'.$databaseUserName.'`@`%`';

DB::unprepared('
DROP TRIGGER IF EXISTS user_religion_details_AFTER_UPDATE;

CREATE '.$definer.' TRIGGER user_religion_details_AFTER_UPDATE AFTER UPDATE ON `'.$databaseName.'.user_religion_details`

FOR EACH ROW

BEGIN
IF (NEW.religion_id <> OLD.religion_id) THEN
UPDATE users SET religion_id = NEW.religion_id WHERE id = OLD.user_id;
END IF;
END
');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP TRIGGER `user_religion_details_AFTER_UPDATE`');
}
}

请告诉我哪里做错了

最佳答案

尝试:

public function up()
{
$database = config('database.default');
$databaseUserName = config('database.connections.'.$database.'.username');

$definer = 'DEFINER=`'.$databaseUserName.'`@`%`';

DB::unprepared('DROP TRIGGER IF EXISTS user_religion_details_AFTER_UPDATE');
DB::unprepared('CREATE '.$definer.' TRIGGER user_religion_details_AFTER_UPDATE AFTER UPDATE ON `'.$databaseName.'.user_religion_details`
FOR EACH ROW
BEGIN
IF (NEW.religion_id <> OLD.religion_id) THEN
UPDATE users SET religion_id = NEW.religion_id WHERE id = OLD.user_id;
END IF;
END
');
}

让我知道它是否有效?

关于migration - Laravel,通过迁移创建触发器运行成功,但在 mysql 中找不到触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49248209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com