简体   繁体   English

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

[英]Migration laravel 5.5 : cannot create table except users table and password_reset table

i have 9 tables, when i run command :我有 9 个表,当我运行命令时:

php artisan migrate php工匠迁移

only users table, migration table and password_reset table are created in my database.在我的数据库中只创建了用户表、迁移表和密码重置表。 this is my sample code这是我的示例代码

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'); } }

please help me..?请帮帮我..?

The migration files need to be migrated in right order.迁移文件需要按正确的顺序迁移。 You can't migrate a table with a non existing foreign key.您不能迁移具有不存在的外键的表。

You probably have a foreign key in some migration and the id key for that will come to existence later in the next migration file.您可能在某些迁移中有一个外键,而它的 id 键稍后将在下一个迁移文件中出现。 That is why you get this error.这就是您收到此错误的原因。

Check your migration files and watch out on order they get created.检查您的迁移文件并注意它们的创建顺序。

For example if you have a foreign key alats_merk_id_foreign then the migration file with alats_merk_id must be migrated before.例如,如果您有一个外键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');
});

First create only table and then create foreign key.首先只创建表,然后创建外键。 And make sure merk_id_table should migrate first.并确保应先迁移 merk_id_table。

if you did every thing in all answers如果你在所有答案中都做了每件事

I think your problem is from the foreign/primary key type and length我认为您的问题来自外键/主键类型和长度

so you may create the id as integer and its corresponding foreign key as an integer with different length, or even as another type as string forexample因此,您可以将 id 创建为整数,并将其相应的外键创建为具有不同长度的整数,甚至可以创建为另一种类型的字符串,例如

so try to make integers with unsigned and same length所以尝试使用无符号和相同长度的整数

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

solution:解决方法:

to make the pimary key and its corresponding foreign key in the same exact type and length使主键及其对应的外键的类型和长度完全相同

Try doing it like this:尝试这样做:


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

You need to replicate the above for every migration.您需要为每次迁移复制上述内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 第一个文件/运行后迁移失败(创建用户和密码重置表的文件除外) - Laravel Migration Failed After The First File/Run(except file for creating users and password_reset table) ResetPasswordController 如何使用 password_reset 表 Laravel - How ResetPasswordController works with password_reset table Laravel 如何生成令牌,该令牌将在一段时间后过期,就像laravel 5.1中password_reset表中的令牌字段一样? - How can i generate a token which will expire after some time just like the token field in password_reset table in laravel 5.1? Laravel,如何将密码重置为用户表中重复的电子邮件 - Laravel, How to reset password to email where duplicated in users table Laravel迁移找不到表 - Laravel migration cannot find table 如何在Laravel 5中创建表迁移 - How to create table migration in Laravel 5 Laravel 5.5 —用于经过身份验证的用户和来宾用户的单独表 - Laravel 5.5 — Separate table for authenticated users and guest users 密码重置表列名覆盖 Laravel 5 - password reset table column name override laravel 5 Laravel-如何使用除用户表以外的其他表进行登录? - Laravel - How to use other table except users table for login? Laravel 5.5 Auth ::尝试使用Wordpress表-未定义索引:密码 - Laravel 5.5 Auth::Attempt with a Wordpress Table - Undefined index: password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM