M
M
mix123452020-12-03 13:12:02
SQL
mix12345, 2020-12-03 13:12:02

How to fix migration error?

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)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Sarvarov, 2020-12-03
@megakor

Your category_id field type in the blog_posts table does not match the id field type in the blog_categories table.

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

// На:
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on($tables['categories'])->onDelete('cascade');

J
jazzus, 2020-12-03
@jazzus

Fix it everywhere
$table->increments('id');
$table->id();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question