0
votes

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

2
why do you use foreign in your ticket and your order lines id... you only need to use one foreign.. you are using relation,, so whenever you use in ticket or in order lines.. you can get them with the same id.. lets check my answerSaengdaet
Thank you ! that was the problem. I was using foreign key with references in both migrations (tickets and order_lines) and as you said one is enough. Last question : Does it make a difference if a put the foreign key with reference in Ticket or Order_lines ? Thank again for all your answer.bruno
perhaps this could be a good example of a circular reference error. To answer your question, note that a foreign key cannot reference a column that does not (yet) exists. Which of these 2 tables have records that are created first? The other table with the least priority or that you could consider optional should be the one holding the foreign key.Vincent Edward Gedaria Binua

2 Answers

1
votes

Move foreign key definitions to second function Schema::table(... or since you are creating constraints between multiple tables, add foreign keys in next migration, after creating all required tables.

Remember that, while creating foreign keys, you need to have coresponding tables properly formed up, in the first place.

As a side note, model relationships do not need to be created, while building your db structure. You can safely create them afterwards...

0
votes

I faced issues like this before. Simply change $table->integer('order_line_id')->unsigned(); to $table->unsignedInteger('order_line_id'); same with all other foreign ids (order_id and ticket_id)