迁移 laravel 5.5 : 无法创建 table 除了用户 table 和 password_reset table
Migration laravel 5.5 : cannot create table except users table and password_reset table
我有 9 tables,当我 运行 命令时:
php artisan migrate
我的数据库中仅创建了用户 table、迁移 table 和 password_reset table。这是我的示例代码
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAlatsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
$table->integer('kategori_id')->unsigned();
$table->integer('operator_id')->unsigned();
$table->string('nama');
$table->string('no_plat',15);
$table->date('tahun');
$table->string('volume',20);
$table->text('keterangan');
$table->enum('status',['ada','disewa','servis']);
// $table->timestamp('created_at');
// $table->timestamp('updated_at');
$table->timestamps();
$table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
$table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
});
}
public function down()
{
Schema::dropIfExists('alats');
}
}
请帮帮我..?
迁移文件需要按正确的顺序迁移。您无法迁移具有不存在的外键的 table。
您可能在某些迁移中有一个外键,并且它的 id 键稍后会在下一个迁移文件中存在。这就是您收到此错误的原因。
检查您的迁移文件并注意它们的创建顺序。
例如,如果您有外键 alats_merk_id_foreign
,则必须先迁移带有 alats_merk_id
的迁移文件。
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
});
Schema::table('alats', function(Blueprint $table)
{
$table->foreign('merk_id')
->references('merk_id')->on('merk_id_table_name')
->onDelete('cascade');
});
首先只创建table,然后再创建外键。
并确保 merk_id_table 应该首先迁移。
如果你做了所有答案中的每件事
我认为您的问题出在 foreign/primary 键 类型和长度
因此您可以将 id 创建为整数,并将其对应的外键创建为具有不同长度的整数,甚至可以将其创建为另一种类型,例如字符串
所以尝试生成无符号且长度相同的整数
尝试使用->unsigned()->length(10)...
解决方案:
使主键与其对应的外键具有完全相同的类型和长度
尝试这样做:
Schema::disableForeignKeyConstraints();
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
$table->integer('kategori_id')->unsigned();
$table->integer('operator_id')->unsigned();
$table->string('nama');
$table->string('no_plat',15);
$table->date('tahun');
$table->string('volume',20);
$table->text('keterangan');
$table->enum('status',['ada','disewa','servis']);
// $table->timestamp('created_at');
// $table->timestamp('updated_at');
$table->timestamps();
$table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
$table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
});
Schema::enableForeignKeyConstraints();
您需要为每次迁移复制以上内容。
我有 9 tables,当我 运行 命令时:
php artisan migrate
我的数据库中仅创建了用户 table、迁移 table 和 password_reset table。这是我的示例代码
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAlatsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
$table->integer('kategori_id')->unsigned();
$table->integer('operator_id')->unsigned();
$table->string('nama');
$table->string('no_plat',15);
$table->date('tahun');
$table->string('volume',20);
$table->text('keterangan');
$table->enum('status',['ada','disewa','servis']);
// $table->timestamp('created_at');
// $table->timestamp('updated_at');
$table->timestamps();
$table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
$table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
});
}
public function down()
{
Schema::dropIfExists('alats');
}
}
请帮帮我..?
迁移文件需要按正确的顺序迁移。您无法迁移具有不存在的外键的 table。
您可能在某些迁移中有一个外键,并且它的 id 键稍后会在下一个迁移文件中存在。这就是您收到此错误的原因。
检查您的迁移文件并注意它们的创建顺序。
例如,如果您有外键 alats_merk_id_foreign
,则必须先迁移带有 alats_merk_id
的迁移文件。
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
});
Schema::table('alats', function(Blueprint $table)
{
$table->foreign('merk_id')
->references('merk_id')->on('merk_id_table_name')
->onDelete('cascade');
});
首先只创建table,然后再创建外键。 并确保 merk_id_table 应该首先迁移。
如果你做了所有答案中的每件事
我认为您的问题出在 foreign/primary 键 类型和长度
因此您可以将 id 创建为整数,并将其对应的外键创建为具有不同长度的整数,甚至可以将其创建为另一种类型,例如字符串
所以尝试生成无符号且长度相同的整数
尝试使用->unsigned()->length(10)...
解决方案:
使主键与其对应的外键具有完全相同的类型和长度
尝试这样做:
Schema::disableForeignKeyConstraints();
Schema::create('alats', function (Blueprint $table) {
$table->increments('id');
$table->integer('merk_id')->unsigned();
$table->integer('kategori_id')->unsigned();
$table->integer('operator_id')->unsigned();
$table->string('nama');
$table->string('no_plat',15);
$table->date('tahun');
$table->string('volume',20);
$table->text('keterangan');
$table->enum('status',['ada','disewa','servis']);
// $table->timestamp('created_at');
// $table->timestamp('updated_at');
$table->timestamps();
$table->foreign('merk_id')->references('id')->on('merks')->onDelete('CASCADE');
$table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('CASCADE');
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('CASCADE');
});
Schema::enableForeignKeyConstraints();
您需要为每次迁移复制以上内容。