【问题标题】:Laravel 5.2 [PDOException] SQLSTATE[42000] - creates two auto_increment primary keys fieldsLaravel 5.2 [PDOException] SQLSTATE[42000] - 创建两个 auto_increment 主键字段
【发布时间】:2016-03-09 06:49:36
【问题描述】:

您好,我有两个迁移表,第一个用于创建客户架构:

Schema::create('customers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name',45);
        $table->string('middle_name',45)->nullable();
        $table->string('last_name',45);
        $table->string('gender',45);
        $table->string('dob',45);
        $table->string('martial_status',45);
        $table->string('home_phone',12)->nullable();
        $table->string('mobile_phone',12);
        $table->string('work_phone',12);
        $table->string('id_number',12);
        $table->string('id_type',45);
        $table->string('id_exp',20);
        $table->date('id_issue_date');
        $table->text('id_image')->nullable();
        $table->timestamps();

第二个是用于创建 customer_address 架构:

   Schema::create('customers_address', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('customer_id')->unsigned();
        $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
        $table->string('address_line1',20);
        $table->string('address_line2',20)->nullable();
        $table->string('ownership',20);
        $table->string('country');
        $table->string('town_city');
        $table->string('parish_state');
        $table->integer('years_at_address',10);
        $table->timestamps();
    });

运行 php artisan migrate 时,我的终端出现错误提示 [PDOException] SQLSTATE[42000]:语法错误或访问冲突:1075 表定义不正确;只能有一个自动列,并且必须将其定义为键。

我得到这个的原因是什么,因为我看不出我在这里做错了什么。提前感谢您的帮助。

【问题讨论】:

    标签: php mysql laravel-5


    【解决方案1】:

    $table->integer('years_at_address',10); 应该是$table->integer('years_at_address'); 第二个参数是布尔值,如果为真,则整数被视为主键。

    这是 Laravel 框架中 BluePrint 类中整数函数的样子:

    public function integer($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
    }
    

    因此,通过将其设置为 10,您告诉 Laravel 迁移功能您希望将此列视为 autoIncrement 并最终作为主键。

    【讨论】:

    • 谢谢,不敢相信我没有看到。
    猜你喜欢
    • 2013-07-26
    • 2016-05-10
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多