我试着在最后一个php和laravel 8中创建论坛。我在udemy购买了laravel 8的课程,我跟随他的视频,但在我的电脑中有错误,在视频中没有。
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构建论坛laravel 8。
发布于 2021-11-19 19:10:54
问题是,对于posts表的迁移是在讨论迁移之前运行的。
这是因为Laravel运行迁移文件名中的时间戳命令的迁移:
2021_11_13_000535_create_posts_table -> 13. November
2021_11_19_165302_create_discussions_table -> 19. November因此,还没有像我的评论所建议的那样创建dicsussions表!
解决方案很简单,请将文件名更改为:
2021_11_20_000535_create_posts_table -> 20. November
下一次,请按照我前面的建议查看一下您的DB。
来自他们的文档:
生成移动
您可以使用make:migration命令来生成数据库迁移。新的迁移将放在数据库/迁移目录中。每个迁移文件名都包含一个时间戳,允许Laravel确定迁移的顺序:
https://laravel.com/docs/8.x/migrations#generating-migrations
https://stackoverflow.com/questions/70039259
复制相似问题