2015-01-15 17 views
5

Xin chào, tôi đang cố gắng tạo 2 bảng trong DB có tên "thương hiệu" & "sản phẩm". Tôi đã tạo ra tập tin chuyển đổi cho "thương hiệu" được gọi là "create_brand" có chứa:Lỗi cú pháp hoặc lỗi vi phạm truy cập khi chạy "di chuyển nghệ nhân php" cho tệp di chuyển chứa khóa chính và khóa ngoài

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'); 
} 

Các tập tin chuyển đổi cho "sản phẩm" được gọi là "create_product" chứa:

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'); 
} 

Bây giờ khi tôi chạy "php nghệ nhân di chuyển "tôi gặp lỗi:

[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 

Tôi sẽ rất biết ơn nếu ai đó có thể giúp tôi giải quyết vấn đề này :) Cảm ơn.

+1

Tại sao hai lần 'Schema :: tạo ('product'' – lukasgeiter

+0

'-?> OnDelete (' thác')' không đòi hỏi? –

Trả lời

6

Sử dụng Schema::create chỉ một lần. Để chỉnh sửa việc sử dụng bảng Schema::table

Và khi sử dụng khóa ngoại, loại phải là unsignedInteger. Nếu bạn tạo một số nguyên cơ bản và cố gắng đặt một khóa ngoại vào nó, nó sẽ thất bại.

Thay đổi mã của bạn để

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

Đối với tôi mã dưới đây là hoạt động 100%.
Trong AppServiceProvider.php

use Illuminate\Support\Facades\Schema; 
public function boot() 
{ 
    Schema::defaultStringLength(191); 
} 
Các vấn đề liên quan