2016-07-18 18 views
3

hinzufügen Ich versuche, einen Nullable Fremdschlüssel des Char-Datentyps zu erstellen. Wenn ich den Migrationsbefehl ausführe. Ich erhalte den folgenden Fehler. Ich bin mir nicht sicher, wo ich falsch liege.Laravel 5.2 Migration: Kann keinen Fremdschlüssel von char Datentyp

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table levels add constraint levels_sample_type_id_foreign foreign key (sample_type_id) references sample_types (id))

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

sind hier die Migration Dateiinhalte für Tabellen Ebene

public function up() 
{ 
    Schema::create('levels', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->char('id', 36)->unique(); 
     $table->string('description')->nullable(); 
     $table->char('sample_type_id', 36)->nullable(); 
     $table->integer('order'); 
     $table->timestamps(); 
     $table->primary('id'); 
     $table->foreign('sample_type_id')->references('id')->on('sample_types'); 
    }); 
} 

und für die sample_types Tabelle sind als

public function up() 
{ 
    Schema::create('sample_types', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->char('id', 36)->unique(); 
     $table->string('name'); 
     $table->timestamps(); 
     $table->primary('id'); 
    }); 
} 
+0

Welcher Typ ist die Spalte 'sample_types.id'? – miken32

+0

Char der Größe 36 –

+0

Ich bin ziemlich positiv, beide müssen vom gleichen Typ sein. Daher darf 'sample_type_id' nicht null sein. –

Antwort

2
folgt

Die Fremdschlüssel-Anweisung fehl, wenn die Tabelle bezeichnet wird existiert noch nicht. Stellen Sie sicher, dass Sie die Tabelle sample_types erstellen, bevor Sie in der Migrationsdatei darauf verweisen.

Verwandte Themen