I spent already hell of a lot of time but still doesn't work my migration with foreign keys alter. I get
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your S
QL syntax; check the manual that corresponds to your MySQL server version for the r
ight syntax to use near '1' at line 1 (SQL: alter table `posts` add constraint post
s_author_foreign foreign key (`author`) references `users` (`id`) on delete 1)
the class what I try to migrate look like
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
if (!Schema::hasTable('posts')) {
Schema::create('posts', function($table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('title', 255);
$table->string('slug', 255);
$table->unique('slug');
$table->string('type', 255);
$table->text('content');
$table->integer('parent');
$table->integer('author')->unsigned();
$table->string('avatar', 255);
$table->string('guid', 255);
$table->string('mime_type', 255);
$table->integer('menu_order');
$table->boolean('status');
$table->index('id');
$table->timestamps();
});
Schema::table('posts', function($table) {
$table->foreign('author')->references('id')->on('users')->onDelete();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('posts');
}
}
what is wrong in my migration?
The error says it all.
Maybe you wanted this:
$table->foreign('author')->references('id')->on('users')->onDelete();
to be:
$table->foreign('author')->references('id')->on('users')->onDelete('cascade');
or maybe simply:
$table->foreign('author')->references('id')->on('users');
See the documentations here.