menu

arrow_back Artisan migrate error?

by
1 vote
When installing foreign keys is not migrated, writes "General error: 1005 Can't create table", while if you create without foreign key, and then manually add it to the database, everything is OK.
Migrations:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
$table->string('social_service');
$table->integer('social_service_id')->unsigned();
$table->enum('role', array('administrator', 'organizer', 'customer'));
});

Schema::create('providers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('site');
$table->timestamps();
});

Schema::create('purchases', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->notNull();
$table->string('name');
$table->integer('provider_id')->unsigned()->notNull();
$table->string('payment_method');
$table->date('stop_date');
$table->integer('min_amount')->unsigned();
$table->integer('organizer_percent')->unsigned();
$table->string('logo');
$table->text('description');
$table->boolean('show_in_web');
$table->boolean('show_in_vk');
$table->integer('status');

$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('cascade')
->onDelete('cascade');

$table->foreign('provider_id')
->references('id')
->on('providers')
->onUpdate('cascade')
->onDelete('cascade');

$table->timestamps();
});
It is the last foreign key (provider_id) that is being swore at. What can it be?

1 Answer

by
0 votes
This migration worked fine for me on a test base.

Maybe this is because older versions of MySQL create myISAM tables by default (they do not support foreign keys). You can try manually setting the engine before operations:

$table->engine = 'InnoDB';

Creation by hand - how exactly? Maybe the editor overrides the engine itself.

Read more

3 Comments

No, there's nothing wrong with the engine.
kirill-93 : Well then, of course. You must first have a provision created to refer to it externally.
indeed, if you add in one migration, it works. But I get 3 files with migrations. Maybe the order has some effect there?