2015-11-09 2 views
1

У меня есть перенести этот кодошибки внешнего ключа в миграции Laravel 5.1

public function up() 
{ 
    Schema::create('TechnicianGroup', function(Blueprint $table){ 
     $table->increments('id')->unsigned(); 
     $table->string('name', 255); 
     $table->string('description', 64); 
     $table->string('token', 255); 
     $table->timestamps(); 
    }); 
    Schema::create('Site', function(Blueprint $table){ 
     $table->increments('id'); 
     $table->string('name', 255); 
     $table->string('token', 255); 
     $table->timestamps(); 
    }); 
    Schema::create('technician', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name', 50); 
     $table->string('username', 50); 
     $table->string('password', 255); 
     $table->string('email', 255); 
     $table->string('mobile', 11); 
     $table->string('avatar_url', 256); 
     $table->string('job_title', 100); 
     $table->integer('site_id'); 
     $table->foreign('site_id')->references('id')->on('site')->onDelete('cascade'); 
     $table->integer('group_id'); 
     $table->boolean('is_active'); 
     $table->string('token', 255); 
     $table->timestamps(); 
    }); 

} 

Когда я migarate

преобразования приложений
php artisan migrate 

В результате

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table 'upport.#sql-a3c_140' (errno: 150) (SQL: alter table technician add constraint technician_site_id_foreign foreign key (site_id) referen ces site (id)) [PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table 'upport.#sql-a3c_140' (errno: 150)

ответ

1

Вы забыли использовать unsigned(). Если вы не используете unsinged(), столбец будет int(11), но вам нужно int(10).

site.id и technician.site_id должны быть того же типа: INT (10)

Попробуйте это:

$table->foreign('site_id')->unsigned()->references('id')->on('site')->onDelete('cascade'); 
+0

Благодаря попробовать этот код , но не работает – paranoid

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