MonkeyBusiness
MonkeyBusiness

Reputation: 593

Create two table with laravel migration with references

I need to create two tables: First articles:

public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->timestamp('roba_spremna');
            $table->timestamp('auction_end');
            $table->string('key');

            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

and then offers:

 public function up()
    {
        Schema::create('offers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('article_id');
            $table->integer('price');
            $table->string('comment');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('article_id')
                ->references('id')
                ->on('articles')
                ->onDelete('cascade');
        });
    }

When I run php artisan migrate I get:

enter image description here

What is the problem? Why I cant create succesfully this two tables?

Upvotes: 1

Views: 686

Answers (2)

Nehal Hasnayeen
Nehal Hasnayeen

Reputation: 1223

If you want to reference id column of a table your column needs to be unsigned, the article_id column in offers table is not unsigned , make it unsigned and refresh the migration

Upvotes: 1

Farshid Ashouri
Farshid Ashouri

Reputation: 17701

It's because table is already created. Delete table and re run.

Upvotes: 1

Related Questions