我正在使用Laravel框架,我试图将一些文件迁移到数据库(phpmyadmin),但我收到此错误:
[照亮\数据库\ QueryException]
SQLSTATE [HY000]:一般错误:1005无法创建表格' loja。#sql-38f_25f' (错误:150)(SQL:alter table encomenda
添加c
onstraint encomenda_username_foreign外键(username
)引用users
(username
))
这是我的表" encomenda":
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEncomendaTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Creates the encomenda table
Schema::create('encomenda', function($table)
{
$table->engine = 'InnoDB';
$table->increments('IDEncomenda')->unsigned();
$table->integer('id')->unsigned();
$table->foreign('id')->references('id')->on('users')->onDelete('cascade');
$table->string('editora');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('encomendas');
}
}
在错误消息中,它表示无法创建表格,但是此文件中的位置没有引用“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');
}
}
现在这是我的桌子&#39; linhaItem&#39;迁移:
<?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');
}
}
有人知道什么是错的吗?
编辑:我将 - &gt; onDelete(&#39; cascade&#39;)添加到外键,但是我得到了同样的错误。
EDIT2:我添加了unsigned到&#39; encomenda&#39;中的id列。文件,但现在我仍然得到相同的错误,但使用&#39; linhaItem&#39;表迁移。
答案 0 :(得分:0)
你有几个问题:
users
迁移:
$table->unique('username');
语句用于创建唯一索引,而不是列:http://laravel.com/docs/4.2/schema#adding-indexes
将其更改为:$table->string('username')->unique();
- 这将为其添加列和唯一索引。
encomenda
迁移:
$table->string('username')->unsigned();
:unsigned
仅用于整数,您无法使字符串无符号。
使用与users
迁移相同的代码:$table->string('username')->unique();
答案 1 :(得分:0)
我解决了这个问题。问题是我正在尝试迁移表,这些表具有尚未迁移的表的外键。我只是重命名了所有文件,将它们放入一个可行的顺序。