2016-03-27 3 views
2
[email protected]:~/www/laravel$ php artisan migrate 

При запуске кода не создается последняя таблица базы данных.Ошибка создания таблиц Laravel

Работала:

  • роли
  • разрешения
  • permission_role

Ошибка:

  • role_user

enter image description here

Это перекодировать мою базу данных миграции ..

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

class CreateRolePermissions extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('roles', function(Blueprint $table){ 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('label'); 
      $table->timestamps(); 

     }); 

     Schema::create('permissions', function(Blueprint $table){ 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('label'); 
      $table->timestamps(); 

     }); 

     Schema::create('permission_role', function(Blueprint $table){ 
      $table->integer('role_id')->unsigined(); 
      $table->integer('permission_id')->unsigined(); 

      $table->foreign('role_id') 
       ->referance('id') 
       ->on('roles') 
       ->onDelete('cascade'); 

      $table->foreign('permission_id') 
       ->referance('id') 
       ->on('permissions') 
       ->onDelete('cascade'); 

      $table->primary(['role_id', 'permission_id']); 

     }); 

     Schema::create('role_user', function(Blueprint $table){ 
      $table->integer('role_id')->unsigined(); 
      $table->integer('user_id')->unsigined(); 

      $table->foreign('role_id') 
       ->referance('id') 
       ->on('roles') 
       ->onDelete('cascade'); 

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

      $table->primary(['role_id', 'user_id']); 

     }); 

    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     \DB::statement('SET FOREIGN_KEY_CHECKS = 0'); 

     Schema::drop('roles'); 
     Schema::drop('permissions'); 
     Schema::drop('permission_role'); 
     Schema::drop('role_user'); 

     \DB::statement('SET FOREIGN_KEY_CHECKS = 1'); 

    } 
} 

Я был бы рад, если бы вы мне помочь я решить эту проблему долгое время.

Спасибо.

ответ

3

Это должно быть ->references(), но не ->referance().

https://laravel.com/docs/5.1/migrations#foreign-key-constraints

+0

Привет Алексия, [Осветите \ Database \ QueryException] SQLSTATE [HY000]: Общая ошибка: 1215 Невозможно добавить ограничение внешнего ключа (SQL : изменить таблицу 'permission_role' добавить ограничение' permission_role_role_id_for eign' иностранных key ('role_id') ссылки' role' ('id')) и [PDOException] SQLSTATE [HY000]: Общая ошибка: 1215 Невозможно добавить ограничение внешнего ключа Спасибо –

+0

Спасибо, и -> unsigined() to -> unsigned() :) спасибо за помощь –

Смежные вопросы