lundi 31 août 2015

Laravel error with foreign key

When I'm trying to set a relation between two tables I receive the error:

D:\wamp\www>php artisan migrate Migration table created successfully.

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table rittenregistratie add co nstraint rittenregistratie_karakterritid_foreign foreign key (karakterritid) references karakterrit (id) on d elete cascade)

[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

D:\wamp\www>

This is my migration rittenregistratie (it's dutch) :

    <?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRittenregistratieTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rittenregistratie', function (Blueprint $table) 
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->timestamps('datum');
            $table->integer('beginstand');
            $table->integer('eindstand');
            $table->text('van');
            $table->text('naar');
            $table->text('bezoekadres');
            $table->text('geredenroute');
            $table->integer('karakterritid')->default(1);
            $table->text('toelichting');
            $table->integer('kilometerszakelijk');
            $table->integer('kilomteresprive');

            $table->foreign('user_id')
                        ->references('id')
                        ->on('users')
                        ->onDelete('cascade');

            $table->foreign('karakterritid')
                        ->references('id')
                        ->on('karakterrit')
                        ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('rittenregistratie');
    }
}

This is where I want to relate to:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateKarakterritTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('karakterrit',function(Blueprint $table)
        {
            $table->increments('id');
            $table->text('rit');          
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('karakterrit');
    }
}

What is am I doing wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1KyKYtI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire