multiple foreign key that is also primary key in schema builder( laravel )

隐身守侯 提交于 2019-12-13 19:23:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!