简体   繁体   中英

Laravel Error : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists

I cant figure out what the problem is, the 2 tables are not connecting for some reason, I read many articles and tried many things still not working.

I want to link post and category tables together, so when I can display the category chosen in the post made.

  public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('name');
            $table->text('description');
            $table->integer('category_id');
            $table->integer('price');
            $table->integer('currency_id');
        });
    }

Category

 public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name');
            $table->bigInteger('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts');
        });
    }

This is the error I get:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists (SQL: create table categories ( id bigint unsigned not null auto_increment primary key, created_at timestamp null, updated_at timestamp null, name varchar(255) not null, post_id bigint unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

Try refreshing you database entirely using the migrate:refresh artisan command.

php artisan migrate:refresh --seed

It may be that a database migration ran and failed before it could register in the migrations table of your database.


Issues: (so far)

1) As per above, a migrate:refresh sorts out the original error

2) $table->bigInteger('post_id')->unsigned(); will not work as posts.id is an integer and not a bigInteger .

Solution:

Change your post_id definition to

$table->integer('post_id')->unsigned();

It says the categories table already exists. So what you have to do is, If you are in dev env, you can delete the table and try it or you can do like the below command.

Run artisan like this, it will drop all tables and migrate fresher.

php artisan migrate:fresh

This worked for me.

The answer is very simple, you have to change $table->increments('id') in posts table to $table->id() , because the foreign key must refer to a primary key as the message says.

Here are some tips for you

  • You have to use bigIncrements in the posts table instead integer because the length of integer is 4-byte but the bigIncrements is 8-byte and this may cause a problem in the future

  • You may love to use this line

$table->foreignId('post_id')->constrained();

instead

$table->bigInteger('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');

for simplicity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM