“General error: 1005 Can't create table” Using

2020-01-31 02:39发布

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?

11条回答
该账号已被封号
2楼-- · 2020-01-31 03:18

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.

查看更多
Ridiculous、
3楼-- · 2020-01-31 03:18

when using foreign key-s make sure that your foreign key is unsigned. this worked for me

查看更多
Emotional °昔
4楼-- · 2020-01-31 03:24

I've been having the same problem. I just noticed the following note at the very bottom of the Laravel Schema docs:

Note: The field referenced in the foreign key is very likely an auto increment and therefore automatically an unsigned integer. Please make sure to create the foreign key field with unsigned() as both fields have to be the exact same type, the engine on both tables has to be set to InnoDB, and the referenced table must be created before the table with the foreign key.

For me, as soon as I set my foreign key fields as such:

$table->integer('author')->unsigned();

I had no problem.

EDIT: Also, make sure that the fields in the foreign table are already created, otherwise this may fail with the same error.

查看更多
我命由我不由天
5楼-- · 2020-01-31 03:25

Here's what I do in Laravel 5:

// CREATING TABLE
Schema::create('your_table_name', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->increments('id'); // index field example sample
    $table->string('field2'); // some varchar/string field sample
    $table->integer('field3'); // some integer field sample
    $table->integer('field_some_table_id')->unsigned; //for field that contains foreign key constraint
});

// FOREIGN KEY CONSTRAINT
Schema::table('stock', function ($table) {
    $table->foreign('field_some_table_id')->references('id')->on('some_table')->onDelete('cascade')->onUpdate('cascade');
});

That's all for the conclusion, and it works for me.

查看更多
疯言疯语
6楼-- · 2020-01-31 03:26

Easiest way is to disable foreign key checks:

DB::statement('set foreign_key_checks=0');

Schema::table( ... );

DB::statement('set foreign_key_checks=1');
查看更多
登录 后发表回答