[英]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)...
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.