我嘗試在最后一個 php 和 laravel 8 中創建論壇。我在 laravel 8 中購買了 udemy 課程,我關注了他的視頻,但在我的電腦中有錯誤,視頻中沒有
2021_11_13_000535_create_posts_table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->integer('is_deleted');
$table->integer('is_approved');
$table->string('image');
$table->unsignedBigInteger('discussion_id');
$table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
2021_11_19_165302_create_discussions_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDiscussionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('discussions', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('desc');
$table->unsignedBigInteger('forum_id');
$table->foreign('forum_id')->references('id')->on('forums')->onDelete('cascade');
$table->integer('is_deleted')->default(0);
$table->string('image')->nullable();
$table->integer('notify')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('discussions');
}
}
當我嘗試遷移表時,出現此錯誤:
Migrated: 2014_10_12_000000_create_users_table (1,399.45ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (2,117.91ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (1,592.76ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated: 2019_12_14_000001_create_personal_access_tokens_table (2,125.96ms)
Migrating: 2021_11_12_234608_create_categories_table
Migrated: 2021_11_12_234608_create_categories_table (2,452.77ms)
Migrating: 2021_11_12_235039_create_forums_table
Migrated: 2021_11_12_235039_create_forums_table (2,849.71ms)
Migrating: 2021_11_13_000340_create_tags_table
Migrated: 2021_11_13_000340_create_tags_table (526.62ms)
Migrating: 2021_11_13_000535_create_posts_table
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `stsdb`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_discussion_id_foreign` foreign key (`discussion_id`) references `discussions` (`id`) on delete cascade)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
699▕ // If an exception occurs when attempting to run a query, we'll format the error
700▕ // message to include the bindings with SQL, which will make this exception a
701▕ // lot more helpful to the developer instead of just the database's errors.
702▕ catch (Exception $e) {
? 703▕ throw new QueryException(
704▕ $query, $this->prepareBindings($bindings), $e
705▕ );
706▕ }
707▕ }
9 vendor frames
10 database/migrations/2021_11_13_000535_create_posts_table.php:28
Illuminate\Support\Facades\Facade::__callStatic()
21 vendor frames
32 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
有人可以解釋為什么會出現這個錯誤以及如何解決這個問題?因為我不明白我在我的電腦中的視頻中有錯誤而在視頻中沒有。有人可以幫助解決這個問題嗎?我已經在谷歌檢查過,但我沒有找到如何解決這個問題,我關注 tuts build forum laravel 8 并帶有電報通知
uj5u.com熱心網友回復:
問題是您對帖子表的遷移是在討論遷移之前運行的。
發生這種情況是因為 Laravel 運行按遷移檔案名中的時間戳排序的遷移:
2021_11_13_000535_create_posts_table -> 13. November
2021_11_19_165302_create_discussions_table -> 19. November
因此,我的評論建議尚未創建討論表!
解決辦法很簡單,把檔案名改成:
2021_11_20_000535_create_posts_table -> 20. November
下次請按照我之前的建議查看您的資料庫。
從他們的檔案:
生成遷移
您可以使用 make:migration Artisan 命令生成資料庫遷移。新的遷移將放置在您的 database/migrations 目錄中。每個遷移檔案名都包含一個時間戳,允許 Laravel 確定遷移的順序:
https://laravel.com/docs/8.x/migrations#generating-migrations
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361085.html
標籤:php mysql 拉拉维尔 外键 laravel-8
下一篇:laravel查詢生成器計數關系
