我正在使用框架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