运行 Php laravel 迁移时出错。
我尝试了 php artisan make:migration rename_name_in_users --table=users ,成功并编辑了以下内容;
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->renameColumn('name', 'username');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->renameColumn('username', 'name');
});
}
};
使用 php artisan migrate 后出现以下错误;
2023_05_31_160724_rename_name_in_users ................................................................ 13ms FAIL
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'column `name` to `username`' at line 1
(Connection: mysql, SQL: alter table `users` rename column `name` to `username`)
at vendor\laravel\framework\src\Illuminate\Database\Connection.php:793
789▕ // If an exception occurs when attempting to run a query, we'll format the error
790▕ // message to include the bindings with SQL, which will make this exception a
791▕ // lot more helpful to the developer instead of just the database's errors.
792▕ catch (Exception $e) {
➜ 793▕ throw new QueryException(
794▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
795▕ );
796▕ }
797▕ }
1 vendor\laravel\framework\src\Illuminate\Database\Connection.php:572
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'column `name` to `username`' at line 1")
2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:572
PDO::prepare("alter table `users` rename column `name` to `username`")
ALTER TABLE t CHANGE COLUMN old_name new_name ((datatype specs))
是 RENAME COLUMN
的旧语法。