简体   繁体   中英

Foreign Key error in migration laravel 5.1

I have migrate this code

 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();
    });

}

When i excute migarate

php artisan migrate

The result is

[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)

You forgot use unsigned() . If you don't use unsinged() , the column will be int(11) , but you need int(10) .

The site.id and technician.site_id must be the same type: int(10)

Try this:

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

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