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?i
I ran into this issue too.
The solution I found is that the tables that contain the id that is being used a foreign id needs to be created before another table can reference it. Basically, you are creating a table and telling MySQL to reference another table's primary key but that table doesn't exist yet.
In your example, the author and lesson tables need to be created first.
The order in which the tables are created is dependent on artisan and the order you created your migration files.
My opinion would be to empty out your database of all the tables and change the timestamps in the migration file names (or delete them and recreate them in the correct order) so that your author and lesson tables are created before your tutorials table.
in my case the errors were same as this.
Error: [Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'nabsh.#sql-5b0_e' (errno: 121) (SQL: alter tableusermeta
add constraint usermeta_uid_foreign foreign key (uid
) ref erencesusers
(id
) on delete cascade).
I found a good tip in this problem's approved answer: ERROR: Error 1005: Can't create table (errno: 121)
Tip: You will get this message if you're trying to add a constraint with a name that's already used somewhere else.
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.
Schema::table('tutorials', function($table)
{
$table->foreign('author')->references('id')->on('users');
$table->foreign('lesson')->references('id')->on('lessons');
});
to ->
Schema::table('tutorials', function($table)
{
$table->foreign('author', 'fk_tutorials_author')->references('id')->on('users');
$table->foreign('lesson', 'fk_tutorials_lesson')->references('id')->on('lessons');
});
i hope it helps. it works in my case
A Summary of the answers already listed, plus mine:
Foreign Keys generally require InnoDb
, so set your default engine, or explicitly specify
$table->engine = 'InnoDB';
Foreign 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).
Easiest way is to disable foreign key checks:
DB::statement('set foreign_key_checks=0');
Schema::table( ... );
DB::statement('set foreign_key_checks=1');
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