2014-12-06 96 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

我解决了这个问题。问题是我试图迁移表,这些表有外键到尚未迁移的表。我只是将所有文件重命名为可以工作的顺序。