问题
I have this schema builder (I omitted the class extension and the schema down part)
public function up()
{
Schema::create('cat', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('path_img');
$table->softDeletes();
});
}
this:
public function up()
{
Schema::create('campo', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->timestamps();
$table->string('nome');
});
}
and this:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign(array('id_campo','id_cat'))->references(array('id','id'))->on(array('campo','cat'));
});
}
What I am trying to do is to state that the table 'campo_cat' has 2 entries that are BOTH primary key and are also foreign key, referenced to cat id and campo id.
1)If I use unsigned when I migrate I get an array to string conversion error exception 2)Is unsigned needed? or I can just define:
$table->unsigned('..');
as both 1) and 2) campo_cat doesn't create
The problem seems to be related to:
$table->foreign(array('id_campo','id_cat'))
->references(array('id','id'))
->on(array('campo','cat'));
EDIT:
I managed to make it work by doing:
public function up()
{
Schema::create('campo_cat', function(Blueprint $table)
{
$table->timestamps();
$table->bigInteger('id_campo')->unsigned();
$table->bigInteger('id_cat')->unsigned();
$table->primary(array('id_campo','id_cat'));
$table->foreign('id_campo')->references('id')->on('campo');
$table->foreign('id_cat')->references('id')->on('cat');
});
}
AND add
->unsigned(); //to the primary key in the other 2 scheme
Still wonder why if i use array it throws an error.
来源:https://stackoverflow.com/questions/22477726/multiple-foreign-key-that-is-also-primary-key-in-schema-builder-laravel