Here's my migration code:
public function up()
{
Schema::create('foos', function(Blueprint $table) {
// Primary key
$table->increments('id');
// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});
Schema::create('bars', function(Blueprint $table) {
// Primary key
$table->increments('id');
// Define foreign key
$table->integer('foo_id')->unsigned;
// Foreign key contraints
// NOTE: causes "General error: 1215 Cannot add foreign key constraint"
// $table->foreign('foo_id')->references('id')->on('foos');
// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('foos');
Schema::drop('bars');
}
When the code to define the foreign key constraint is not commented out, I get the following error on the command line: General error: 1215 Cannot add foreign key constraint.
Any ideas what I am doing wrong?
$table->integer('foo_id')->unsigned;
should be
$table->integer('foo_id')->unsigned();
or you can use short version:
$table->unsignedInteger('foo_id');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With