Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel迁移一对一关系错误_Php_Laravel_Eloquent_Migrate - Fatal编程技术网

Php Laravel迁移一对一关系错误

Php Laravel迁移一对一关系错误,php,laravel,eloquent,migrate,Php,Laravel,Eloquent,Migrate,我有一个关于laravel迁移表的问题。我在两个表之间有一对一的关系。订单和机票。我已将这2个功能放在模型票和订单行中: public function orderline() { return $this->hasOne('App\OrderLine'); } public function ticket() { return $this->hasOne('App\Ticket'); } 并在迁移文件中创建模式表: public function u

我有一个关于laravel迁移表的问题。我在两个表之间有一对一的关系。订单和机票。我已将这2个功能放在模型票和订单行中:

public function orderline()
{
    return $this->hasOne('App\OrderLine');

} 

public function ticket()
{
    return $this->hasOne('App\Ticket');
}    
并在迁移文件中创建模式表:

public function up()
{
    Schema::create('tickets', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('order_line_id')->unsigned(); //foreign key
        $table->string('number');
        $table->float('price');
        $table->timestamps();

        $table->foreign('order_line_id')->references('id')->on('order_lines');//relationship  one to one between orderline & ticket tables
    });
}
  public function up()
{
    Schema::create('order_lines', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('ticket_id')->unsigned();//foreign key
        $table->integer('order_id')->unsigned(); //foreign key
        $table->float('total_order_line');
        $table->timestamps();

        $table->foreign('ticket_id')->references('id')->on('tickets');//relationship  one to one between orderline & ticket tables

        $table->foreign('order_id')// relationship one to many between order & order line tables
        ->references('id')->on('orders')
            ->onDelete('cascade');
    });
}
在迁移文件中创建_顺序_行_表模式:

public function up()
{
    Schema::create('tickets', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('order_line_id')->unsigned(); //foreign key
        $table->string('number');
        $table->float('price');
        $table->timestamps();

        $table->foreign('order_line_id')->references('id')->on('order_lines');//relationship  one to one between orderline & ticket tables
    });
}
  public function up()
{
    Schema::create('order_lines', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('ticket_id')->unsigned();//foreign key
        $table->integer('order_id')->unsigned(); //foreign key
        $table->float('total_order_line');
        $table->timestamps();

        $table->foreign('ticket_id')->references('id')->on('tickets');//relationship  one to one between orderline & ticket tables

        $table->foreign('order_id')// relationship one to many between order & order line tables
        ->references('id')->on('orders')
            ->onDelete('cascade');
    });
}
当我进行php artisan迁移时,仍然存在以下错误:

bruno@bruno-HP-EliteBook-840-G4:~/projetconcert$php artisan迁移 已成功创建迁移表

Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表手势\u音乐会\u眼镜。sql-e08\u 30d错误号:150外键约束格式不正确sql:alter表顺序\u行添加约束顺序\u行\u票证\u id\u外键票证\u id引用票证id


有人知道怎么解决吗?非常感谢。Bruno

将外键定义移动到第二个函数模式::表。。。或者,由于要在多个表之间创建约束,请在创建所有必需的表之后,在下一次迁移中添加外键

请记住,在创建外键时,首先需要正确地建立相应的表


另外,在构建数据库结构时,不需要创建模型关系。您可以在以后安全地创建它们…

我以前遇到过类似的问题。只需更改$table->integer'order\u line\u id'->无符号;到$table->unsignedInteger'order_line_id';与所有其他外国id相同订单id和机票id

谢谢您的快速回答。gestion_concert_Biology是数据库的名称,我不知道为什么它会被写入,无法创建表…但我认为问题来自外键。我遵循您的指示:我删除我的数据库并创建了另一个同名数据库,将所有外键和引用放入注释中,完成php artisan迁移。所有的表都是毫无问题地创建的,我把它从注释中去掉,并完成了php artisan migrate:refresh。我也有同样的错误…@bruno我的想法是将所有外键声明移动到下一次/后续的迁移中,并确保所有这些ID都是未签名的。。。。因此,在迁移过程中,首先创建所有表。然后创建下一次迁移并将外键添加到该表中,或者为每个表创建新的迁移文件,以便仅将外键添加到该特定表中…为什么在工单和订单行id中使用外键。。。你只需要使用一个外键。。您使用的是关系,所以无论何时在票证行或订单行中使用。。您可以使用相同的id获取它们。。让我们看看我的答案谢谢!这就是问题所在。我在迁移票证和订单行中使用外键引用,正如您所说的,一个就足够了。最后一个问题:如果a在票证或订单行中放置带引用的外键,会有区别吗?再次感谢您的回答。也许这是一个循环引用错误的好例子。要回答您的问题,请注意外键不能引用尚未存在的列。这两个表中哪一个表具有首先创建的记录?另一个优先级最低的表,或者可以考虑可选的表应该是持有外键的表。