外键代码在 Laravel 8 的迁移中不起作用
Foreign key codes are not working in migrations of Laravel 8
我尝试创建一个外键,但是它不起作用。
帖子迁移
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("title", 60);
$table->string("description", 200);
$table->text("content");
$table->string("photo");
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign("user_id")->references("id")
->on("users")->onDelete("cascade");
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
用户迁移
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string("name", 30);
$table->string("email")->unique();
$table->string("password");
$table->string("username")->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
我正在尝试为应该通过外键与用户 ID 连接的帖子创建一个“user_id”列。但是,当我迁移时,出现如下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table
mytest_db
.posts
(errno: 150 "Foreign key constraint is incorrectly
formed") (SQL: alter table posts
add constraint
posts_user_id_foreign
foreign key (user_id
) references users
(id
) on delete cascade)
at
W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678
674▕ // If an exception occurs when attempting to run a query, we'll format the error
675▕ // message to include the bindings with SQL, which will make this exception a
676▕ // lot more helpful to the developer instead of just the database's errors.
677▕ catch (Exception $e) { ➜ 678▕ throw new QueryException(
679▕ $query, $this->prepareBindings($bindings), $e
680▕ );
681▕ }
682▕
1
W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table mytest_db
.posts
(errno: 150 "Foreign key constraint
is incorrectly formed")")
2
W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
PDOStatement::execute()
不能在尚不存在的表上定义外键。
Laravel 按文件名顺序执行迁移,在迁移名称前加上时间戳。
确保 CreatePostsTable
迁移在 之后 迁移 CreateUsersTable
。
我尝试创建一个外键,但是它不起作用。
帖子迁移
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("title", 60);
$table->string("description", 200);
$table->text("content");
$table->string("photo");
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign("user_id")->references("id")
->on("users")->onDelete("cascade");
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
用户迁移
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string("name", 30);
$table->string("email")->unique();
$table->string("password");
$table->string("username")->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
我正在尝试为应该通过外键与用户 ID 连接的帖子创建一个“user_id”列。但是,当我迁移时,出现如下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table
mytest_db
.posts
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableposts
add constraintposts_user_id_foreign
foreign key (user_id
) referencesusers
(id
) on delete cascade)at W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678 674▕ // If an exception occurs when attempting to run a query, we'll format the error 675▕ // message to include the bindings with SQL, which will make this exception a 676▕ // lot more helpful to the developer instead of just the database's errors. 677▕ catch (Exception $e) { ➜ 678▕ throw new QueryException( 679▕ $query, $this->prepareBindings($bindings), $e 680▕ ); 681▕ } 682▕
1
W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create tablemytest_db
.posts
(errno: 150 "Foreign key constraint is incorrectly formed")")2
W:\domains\mytest.uz\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471 PDOStatement::execute()
不能在尚不存在的表上定义外键。
Laravel 按文件名顺序执行迁移,在迁移名称前加上时间戳。
确保 CreatePostsTable
迁移在 之后 迁移 CreateUsersTable
。