0

Laravelの修得用にtwitterのクローンのようなものを作っていますが、php artisan migrate実行時にエラーが発生します。
複数のテーブルから同一の外部キー制約を適用しているからでしょうか?

-create_users_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->string('name');
            $table->string('image')->nullable();;
            $table->string('image_dir')->nullable();;
            $table->text('profile')->nullable();;
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            $table->datetime('login_date');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

-create_tweets_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTweetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tweets', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('user_id')->unsigned();
          $table->integer('reply_tweet_id')->nullable();
          $table->text('content');
          $table->timestamps();

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tweets');
    }
}

-create_favorites_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFavoritesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('favorites', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('tweet_id')->unsigned();
            $table->timestamps();

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

            $table->foreign('tweet_id')
                ->refarences('id')
                ->on('tweets')
                ->onDelete('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('favorites');
    }
}

-実行結果

  [vagrant@localhost laravel_twitter]$ php artisan migrate
  Migration table created successfully.

  [Illuminate\Database\QueryException]                                                                                                                                          
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right s  
  yntax to use near ') on delete cascade' at line 1 (SQL: alter table `favorites` add constraint `favorites_user_id_foreign` foreign key (`user_id`) references `users` () on   
  delete cascade)                                                                                                                                                               



  [PDOException]                                                                                                                                                                
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right s  
  yntax to use near ') on delete cascade' at line 1                                                                                                                             


[vagrant@localhost laravel_twitter]$ 

1 件の回答 1

1

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') on delete cascade' at line 1 (SQL: alter table `favorites` add constraint `favorites_user_id_foreign` foreign key (`user_id`) references `users` () on delete cascade)

) on delete cascade の近くで文法エラーとあるので、SQL: ... に書かれたSQLを見てみると、references ` users` () で列名が指定されていません。

列名を指定している個所を確認すると…

$table->foreign('user_id')
    ->refarences('id') // ここ
    ->on('users')
    ->onDelete('cascade');

refarencesreferences ではありませんか?

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。