我正在使用框架Laravel,并试图迁移一些文件到数据库(phpmyadmin),但我得到这个错误: [Illuminate \ Database \ QueryException] SQLSTATE [HY000]:常规错误:1005无法创建表 '洛哈#SQL-38f_25f。'(错误:150)(SQL:ALTER TABLE encomenda
放入C onstraint encomenda_username_foreign外键(username
)引用users
(username
))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'表迁移。
在创建'encomenda'之前是否已经创建了'user'表? – Kestutis 2014-12-06 20:22:45
@Kestutis是的。 – 2014-12-06 20:25:48
请同时添加'users'表迁移 – Kestutis 2014-12-06 20:55:28