Essentially, I am having the same issue as this guy, minus the table prefix. Because I have no table prefix, his fix does not work. http://forums.laravel.com/viewtopic.php?id=972
I am trying to build a table using Laravel's Schema Builder like this:
Schema::create('lessons', function($table)
{
$table->increments('id');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->timestamps();
});
Schema::create('tutorials', function($table)
{
$table->increments('id');
$table->integer('author');
$table->integer('lesson');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->string('tagline')->nullable();
$table->text('content')->nullable();
$table->text('attachments')->nullable();
$table->timestamps();
});
Schema::table('tutorials', function($table)
{
$table->foreign('author')->references('id')->on('users');
$table->foreign('lesson')->references('id')->on('lessons');
});
The issue is, when I run this code (in a /setup route), I get the following error:
SQLSTATE[HY000]: General error: 1005 Can't create table 'tutorials.#sql-2cff_da' (errno: 150)
SQL: ALTER TABLE `tutorials` ADD CONSTRAINT tutorials_author_foreign FOREIGN KEY (`author`) REFERENCES `users` (`id`)
Bindings: array (
)
Based on posts around the web and the limited documentation available on how to setup Laravel's Eloquent relationships, I'm not sure what I'm doing wrong...
users
already exists and it does have an id
field that is auto_increment
. I am also setting up my models with the proper relationships (belongs_to
and has_many
), but as far as I can tell this is not the issue-- it's the database setup. The DB is InnoDB.
What exactly am I doing wrong with the foreign key?
A Summary of the answers already listed, plus mine:
Foreign Keys generally require
InnoDb
, so set your default engine, or explicitly specifyForeign keys require the referenced table to exist. Make sure the referenced table is created in an earlier migration, prior to creating the key. Consider creating the keys in a separate migration to be sure.
Foreign Keys require the data type to be congruent. Check whether the referenced field is the same type, whether its signed or unsigned, whether it's length is the same (or less).
If you are switching between hand-coding migrations, and using generators, make sure you check the id type you are using. Artisan uses increments() by default but Jeffrey Way appears to prefer integer('id', true).
Laravel5, MySQL55, CentOS65
in my case the errors were same as this.
I found a good tip in this problem's approved answer: ERROR: Error 1005: Can't create table (errno: 121)
in my case this error means the constraint which has been tried to add is already exists somewhere (but i couldn't see them anywhere).
I copied the output error query and execute it within sequel pro, then hopefully I saw the same error again. as the tip says I changed the constraint name to something else then it affected with no error so there's a problem with constraint name builder in laravel5 + MySQL55 + CentOS65.
Solution: try to rename constraints by sending the second parameter at foreign method in table schema.
to ->
i hope it helps. it works in my case
I'm not 100% sure if these are the reasons this is failing but a couple of pointers. If you're using an older version of mySQL as the database, the default table implementation is myISAM that does not support foreign key restraints. As your scripts are failing on the foreign key assignment, you are better off explicitly stating that you want INNODB as the engine using this syntax in Schema's create method.
This should hopefully alleviate the problems you are having.
Also, whilst you can declare foreign keys as an afterthought, I create the foreign keys within the initial schema as I can do an easy check to make sure I've got the right DB engine set.
Hope this helps / solves your problem.
I received the same error because I forgot to set the table type to InnoDB on the referenced table:
This happened to me in Yii Framework 1.x migrations too. Turned out that similarily to the Laravel solutions above it may be caused by a
You have to give the integer an unsigned flag in Laravel 5.4, like this: