I am trying to set up a table commentswith a foreign key into a table userswith building a Laravel 4 schema, for example:
Schema::create('comments', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id');
$table->text('body');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
But when I start the migration, the following error message appears:
[Exception]
SQLSTATE[HY000]: General error: 1005 Can't create table 'streamr.#sql-16fc_89' (errno: 150)
(SQL: alter table `comments` add constraint comments_user_id_foreign foreign key (`user_id`) references `users` (`id`)) (Bindings: array ())
As far as I'm concerned, this is because the column idof the table users int(10), and $table->integer('user_id')creates a column of int(11)a faulty external switch due to incompatible types of columns, however, when I try to set the length of an integer column, which I create, it does not work:
$table->integer('user_id', 10);
Is there any way around this? It seems strange to me that the Laravel schema builder will build a column int(10)for primary keys and not make them compatible with columns integer: /
: , - InnoDB, .