“General error: 1005 Can't create table” Using Laravel Schema Build and Foreign Keys

后端 未结 11 1052
予麋鹿
予麋鹿 2020-12-03 07:02

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

相关标签:
11条回答
  • 2020-12-03 07:15

    You have to give the integer an unsigned flag in Laravel 5.4, like this:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('post_id');
            $table->text('title');
            $table->text('text');
            $table->integer('employee_post_id_number')->unsigned();
            $table->foreign('employee_post_id_number')->references 
            ('employee_id_number')->on('employees');
            $table->timestamps();
        });
    }
    
    0 讨论(0)
  • 2020-12-03 07:16

    I received the same error because I forgot to set the table type to InnoDB on the referenced table:

    $table->engine = 'InnoDB';
    
    0 讨论(0)
  • 2020-12-03 07:16

    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.

    0 讨论(0)
  • 2020-12-03 07:17

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

    0 讨论(0)
  • 2020-12-03 07:18

    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.

    Schema::create('lessons', function($table)
    {
        $table->engine = 'InnoDB';
    
        $table->increments('id');
        $table->string('title')->nullable();
        $table->string('summary')->nullable();
        $table->timestamps();
    });
    

    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.

    Schema::create('tutorials', function($table)
    {
        $table->engine = 'InnoDB';
    
        $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();
    
        $table->foreign('author')->references('id')->on('users');
        $table->foreign('lesson')->references('id')->on('lessons');
    });
    

    Hope this helps / solves your problem.

    0 讨论(0)
  • 2020-12-03 07:21

    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.

    0 讨论(0)
提交回复
热议问题