2014-12-06 90 views
2

我正在使用框架Laravel,並試圖遷移一些文件到數據庫(phpmyadmin),但我得到這個錯誤: [Illuminate \ Database \ QueryException] SQLSTATE [HY000]:常規錯誤:1005無法創建表 '洛哈#SQL-38f_25f。'(錯誤:150)(SQL:ALTER TABLE encomenda放入C onstraint encomenda_username_foreign外鍵(username)引用usersusername))Laravel SQL無法創建表

這是我的表 「encomenda」:

在錯誤消息中,它表示它不能創建表'loja',但是沒有在這個文件中有'loja'的引用。 我確實也想創建一個表「洛哈」,以防萬一,這裏是代碼:

<?php 

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

class CreateLojaTable extends Migration { 

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('loja', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->primary('api_key'); 
    }); 
} 

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

} 

用戶表遷移:

<?php 
use Illuminate\Database\Migrations\Migration; 

class ConfideSetupUsersTable extends Migration { 

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    // Creates the users table 
    Schema::create('users', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id')->unsigned(); 
     $table->string('username')->unique(); 
     $table->string('email'); 
     $table->string('password'); 
     $table->string('confirmation_code'); 
     $table->string('remember_token')->nullable(); 
     $table->boolean('confirmed')->default(false); 
     $table->boolean('admin')->default(false); 
     $table->timestamps(); 
    }); 


    // Creates password reminders table 
    Schema::create('password_reminders', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->string('email'); 
     $table->string('token'); 
     $table->timestamp('created_at'); 
    }); 
} 

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

} 

喏,這就是我的表「linhaItem」遷移:

<?php 

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

class CreateLinhaItemTable extends Migration { 

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('linhaItem', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('IDLinha')->unsigned(); 
     $table->integer('IDDisco')->unsigned(); 
     $table->integer('IDCarrinho')->unsigned(); 
     $table->double('preco'); 
     $table->integer('quantidade'); 
     $table->foreign('IDDisco')->references('IDDisco')->on('disco')->onDelete('cascade'); 
     $table->foreign('IDCarrinho')->references('IDCarrinho')->on('carrinho')-onDelete('cascade'); 
    }); 
} 

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

} 

有人知道什麼是錯?
編輯:我添加了 - > onDelete('cascade')到外鍵,但我得到了同樣的錯誤。
編輯2:我添加了無符號到'encomenda'文件中的id列,但現在我仍然得到相同的錯誤,但與'linhaItem'表遷移。

+0

在創建'encomenda'之前是否已經創建了'user'表? – Kestutis 2014-12-06 20:22:45

+0

@Kestutis是的。 – 2014-12-06 20:25:48

+0

請同時添加'users'表遷移 – Kestutis 2014-12-06 20:55:28

回答

0

你有幾個問題:

  1. users遷移:

    $table->unique('username');語句創建唯一索引,不列:http://laravel.com/docs/4.2/schema#adding-indexes

    它改成這樣:$table->string('username')->unique(); - 這將爲其添加一列和唯一索引。

  2. encomenda遷移:

    $table->string('username')->unsigned();unsigned僅用於與整數,你不能讓一個字符串無符號。

    使用相同的代碼作爲users遷移:$table->string('username')->unique();

+0

感謝您的提示。不幸的是,它仍然沒有解決我的問題:/我將更新代碼 – 2014-12-06 21:29:01

0

我解決了這個問題。問題是我試圖遷移表,這些表有外鍵到尚未遷移的表。我只是將所有文件重命名爲可以工作的順序。