相关疑难解决方法(0)

对于包含主键和外键的迁移文件运行"php artisan migrate"时出现语法错误或访问冲突错误

嗨,我正在尝试在我的数据库中创建名为"品牌"和"产品"的2个表.我为"品牌"创建了名为"create_brand"的迁移文件,其中包含:

public function up()
{
    Schema::create('brand', function($table){
        $table->increments('brand_id');
        $table->string('brand_name', 100);
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('brand');
}
Run Code Online (Sandbox Code Playgroud)

名为"create_product"的"product"的迁移文件包含:

public function up()
{
    Schema::create('product', function($table){
        $table->increments('skuid');
        $table->integer('brand_id')->unasigned();
    });

    Schema::create('product', function($table){
        $table->foreign('brand_id')->references('brand_id')->on('brand')->onDelete('cascade');
    });


}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('product', function($table){
        $table->dropForeign('brand_id');
    });

    Schema::drop('product');
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行"php artisan migrate"时,我收到错误:

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  your …
Run Code Online (Sandbox Code Playgroud)

migration laravel

6
推荐指数
1
解决办法
5848
查看次数

标签 统计

laravel ×1

migration ×1