使用laravel在另一个表中引用主键

时间:2017-03-23 14:08:12

标签: mysql laravel-5 foreign-key-relationship

我正在使用迁移为Laravel项目创建表。这就是它的样子:

let canExecute = true;
function run () {
  if (canExecute) {
    console.log('Im running');
    canExecute = false;
    pause(1000);
  }
};

function pause(s) {
  console.log('Im paused');
    setTimeout(() =>{
      canExecute = true;
    }, s)
};

run();
run();
run();
setTimeout(() =>{
  run();
}, 2000)

我想创建一个包含table3主键值的列,我猜这是一个无符号的大整数,但我不确定。当我使用Integer或无符号整数时,我得到了这个:

Schema::create( 'table1', function( Blueprint $table ){
    $table->increments( 'id' );
}

Schema::create( 'table2', function( Blueprint $table ){
    $table->increments( 'id' );
}

Schema::create( 'table3', function( Blueprint $table ){
    $table->integer( 'table1_id' )->unsigned();
    $table->integer( 'table2_id' )->unsigned();
    $table->foreign( 'table1_id' )->references( 'id' )->on( 'table1' )->onDelete( 'cascade' );
    $table->foreign( 'table2_id' )->references( 'id' )->on( 'table2' )->onDelete( 'cascade' );
    $table->primary( [ 'table1_id', 'table2_id' ] );
}

Schema::create( 'table4', function( Blueprint $table ){
    // Here is my question
    // How do I reference the primary key on table3??
    $table->whatMethodCreatesTheRightTypeOfColumn( 'table3_id' );
    $table->foreign( 'table3_id' )->references( ???? )->on( 'table3' )->onDelete( 'cascade' );
}

0 个答案:

没有答案