@mix12345

Как исправить ошибку миграции?

class CreateBlogTables extends Migration
{
    public function up(): void
    {
        $tables = config('nova-blog.tables');

        Schema::create($tables['categories'], function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug')->unique();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create($tables['posts'], function (Blueprint $table) use ($tables) {
            $table->increments('id');
            $table->string('slug')->unique();
            $table->string('title');
            $table->json('keywords')->nullable();
            $table->string('description')->nullable();
            $table->string('template');
            $table->text('annotation')->nullable();
            $table->text('content')->nullable();
            $table->foreignId('category_id')->constrained($tables['categories'])->onDelete('cascade');
            $table->foreignId('author_id')->constrained($tables['users'])->onDelete('cascade');
            $table->timestamps();
            $table->timestamp('published_at')->useCurrent();

            $table->index('category_id');
            $table->index('author_id');
            $table->index('published_at');
        });

        if (config('database.default') === 'pgsql') {
            DB::statement(sprintf('alter table %s add ts tsvector null', $tables['posts']));
            DB::statement(sprintf('create index %1$s_ts_index on %1$s using gin (ts)', $tables['posts']));
        }

        Schema::create($tables['tags'], function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug')->unique();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create($tables['post_tags'], function (Blueprint $table) use ($tables) {
            $table->foreignId('post_id')->constrained($tables['posts'])->onDelete('cascade');
            $table->foreignId('tag_id')->constrained($tables['tags'])->onDelete('cascade');

            $table->index('post_id');
            $table->index('tag_id');
        });

        Schema::create($tables['comments'], function (Blueprint $table) use ($tables) {
            $table->increments('id');
            $table->string('content', 4000);
            $table->foreignId('post_id')->constrained($tables['posts'])->onDelete('cascade');
            $table->foreignId('author_id')->constrained($tables['users'])->onDelete('cascade');
            $table->timestamps();

            $table->index('post_id');
            $table->index('author_id');
        });
    }

    public function down(): void
    {
        $tables = config('nova-blog.tables');
        Schema::dropIfExists($tables['comments']);
        Schema::dropIfExists($tables['post_tags']);
        Schema::dropIfExists($tables['tags']);
        Schema::dropIfExists($tables['posts']);
        Schema::dropIfExists($tables['categories']);
    }
}

SQLSTATE[HY000]: General error: 3780 Referencing column 'category_id' and refe
renced column 'id' in foreign key constraint 'blog_posts_category_id_foreign' ar
e incompatible. (SQL: alter table `blog_posts` add constraint `blog_posts_catego
ry_id_foreign` foreign key (`category_id`) references `blog_categories` (`id`) o
n delete cascade)
  • Вопрос задан
  • 89 просмотров
Пригласить эксперта
Ответы на вопрос 2
megakor
@megakor
Go/PHP developer | ВКонтакте
У вас тип поля category_id в таблице blog_posts не совпадает с типом поля id в таблице blog_categories.

// Заменить:
$table->foreignId('category_id')->constrained($tables['categories'])->onDelete('cascade');

// На:
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on($tables['categories'])->onDelete('cascade');
Ответ написан
Комментировать
@jazzus
Исправь везде
$table->increments('id');
на
$table->id();
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы