I have a question regarding laravel migration table. I have a relation one to one between 2 tables. OrderLine and Ticket. I have put these 2 function in the model Ticket and OrderLine:
public function orderline()
{
return $this->hasOne('App\OrderLine');
}
public function ticket()
{
return $this->hasOne('App\Ticket');
}
and in migrations file create_tickets_table the Schema :
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
});
}
and in migrations file create_order_lines_table the Schema :
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');
});
}
and when i do php artisan migrate I still have have this error :
bruno@bruno-HP-EliteBook-840-G4:~/projetconcert$ php artisan migrate Migration table created successfully.
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table gestion_concert_spectacle
.#sql-e08_30d
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table order_lines
add constraint order_lines_ticket_id_foreign
foreign key (ticket_id
) references tickets
(id
))
Does somebody have any idea how to solve it ? Thank you. Bruno