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

后端 未结 11 1119
予麋鹿
予麋鹿 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: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.

提交回复
热议问题