运行"php artisan migration"时出现语法错误或访问冲突错误;用于包含主&的迁移文件外键 [英] Syntax error or access violation error while running "php artisan migrate" for migration file containing primary & foreign keys

查看:101
本文介绍了运行"php artisan migration"时出现语法错误或访问冲突错误;用于包含主&的迁移文件外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi i am trying to create 2 tables in my DB named "brand" & "product". I have created migration file for "brand" called "create_brand" that contains:

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');
}

产品"的迁移文件名为"create_product",其中包含:

The migration file for "product" called "create_product" contains:

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');
}

现在,当我运行"php artisan migration"时,出现错误:

Now when i run "php artisan migrate" i get the error:

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  your SQL syntax; check the manual that corresponds to your MySQL server v
  ersion for the right syntax to use near ') default character set utf8 colla
  te utf8_unicode_ci' at line 1 (SQL: create table `product` () default chara
  cter set utf8 collate utf8_unicode_ci)


  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  n your SQL syntax; check the manual that corresponds to your MySQL server v
  ersion for the right syntax to use near ') default character set utf8 colla
  te utf8_unicode_ci' at line 1

如果有人可以帮助我解决这个问题,我将非常感激:)谢谢.

I would be really grateful if someone could help me solve this issue :) Thanks.

推荐答案

仅使用一次Schema::create.要编辑表,请使用Schema::table

Use Schema::create just once. To edit the table use Schema::table

使用外键时,类型应为unsignedInteger.如果创建一个基本整数并尝试在其上放置一个外键,它将失败.

And when using foreign key the type should be unsignedInteger. If you create a basic integer and try to put a foreign key on it, it will fail.

将代码更改为

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

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

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
    Schema::drop('product');
}

这篇关于运行"php artisan migration"时出现语法错误或访问冲突错误;用于包含主&的迁移文件外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆