迁移 laravel 5.5 :除了用户表和password_reset表之外,无法创建表



当我运行命令时,我有 9 个表:

PHP 工匠迁移

在我的数据库中仅创建用户表、迁移表和password_reset表。 这是我的示例代码

 use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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');
    }
}
 

请帮助我..?

迁移文件需要按正确的顺序迁移。不能迁移具有不存在外键的表。

您可能在某些迁移中有一个外键,该外键稍后将在下一个迁移文件中出现。这就是您收到此错误的原因。

检查您的迁移文件并注意它们的创建顺序。

例如,如果您有外键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');
});

首先仅创建表,然后创建外键。并确保merk_id_table应首先迁移。

如果你在所有答案中都做了每件事

我认为您的问题来自外键/主键类型和长度

因此,您可以将 id 创建为整数,将其相应的外键创建为具有不同长度的整数,甚至创建为另一种类型作为字符串例如

所以尝试制作无符号且长度相同的整数

尝试使用->unsigned()->length(10)...

溶液:

使 pimary 键及其相应的外键具有完全相同的类型和长度

尝试这样做:


        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();

您需要为每次迁移复制上述内容。

最新更新