2015-01-15 13 views
5

Hallo, ich versuche, 2 Tabellen in meiner DB namens "Marke" & "Produkt" zu erstellen. Ich habe für „Marke“ Migrationsdatei erstellt namens „create_brand“, die enthält:Syntaxfehler oder Zugriffsverletzungsfehler beim Ausführen von "php artisan migrate" für Migrationsdatei mit Primär- und Fremdschlüsseln

public function up() 
{ 
    Schema::create('brand', function($table){ 
     $table->increments('brand_id'); 
     $table->string('brand_name', 100); 
    }); 
} 

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

Die Migrationsdatei für „Produkt“ genannt „create_product“ enthält:

public function up() 
{ 
    Schema::create('product', function($table){ 
     $table->increments('skuid'); 
     $table->integer('brand_id')->unasigned(); 
    }); 

    Schema::create('product', function($table){ 
     $table->foreign('brand_id')->references('brand_id')->on('brand')->onDelete('cascade'); 
    }); 


} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::table('product', function($table){ 
     $table->dropForeign('brand_id'); 
    }); 

    Schema::drop('product'); 
} 

Jetzt, als ich „laufen php Handwerker wandern“i den Fehler:

[Illuminate\Database\QueryException] 
    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i 
    your SQL syntax; check the manual that corresponds to your MySQL server v 
    ersion for the right syntax to use near ') default character set utf8 colla 
    te utf8_unicode_ci' at line 1 (SQL: create table `product`() default chara 
    cter set utf8 collate utf8_unicode_ci) 


    [PDOException] 
    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i 
    n your SQL syntax; check the manual that corresponds to your MySQL server v 
    ersion for the right syntax to use near ') default character set utf8 colla 
    te utf8_unicode_ci' at line 1 

ich wirklich dankbar wäre, wenn mir jemand dieses Problem :) Danke lösen helfen könnte.

+1

Warum zweimal 'Schema :: create ('product'' – lukasgeiter

+0

' -> onDelete (' Kaskade ') 'erfordern nicht wahr? –

Antwort

6

Verwenden Sie Schema::create nur einmal. Um die Tabelle zu bearbeiten, verwenden Sie Schema::table

Und wenn Sie Fremdschlüssel verwenden, sollte der Typ unsignedInteger sein. Wenn Sie eine einfache Ganzzahl erstellen und versuchen, einen Fremdschlüssel darauf zu setzen, schlägt sie fehl.

Ihre Codes ändern zu

public function up() 
{ 
    Schema::create('product', function($table){ 
     $table->increments('skuid'); 
     $table->unsignedInteger('brand_id'); 
    }); 

    Schema::table('product', function($table){ 
     $table->foreign('brand_id')->references('brand_id')->on('brand'); 
    }); 
} 

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

Für mich die folgenden Code funktioniert 100%.
In AppServiceProvider.php

use Illuminate\Support\Facades\Schema; 
public function boot() 
{ 
    Schema::defaultStringLength(191); 
} 
Verwandte Themen