Laravel数据迁移中int类型遇到的问题

遇到问题的语句

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('user_id');
            $table->string('user_name', 20)->comment('用户名');
            $table->string('user_password', 255)->comment('用户密码');
            $table->string('user_portrait', 255)->comment('用户头像')->nullable();
            $table->string('user_intro', 255)->comment('用户简介')->nullable();
            $table->string('user_sex', 3)->default(1)->comment('用户性别:0=>保密,1=>男,2=>女');
            $table->integer('user_status', 3)->default(1)->comment('用户状态:1=>启用,0=>禁用');
            $table->string('user_intro', 255)->comment('用户简介');
            $table->integer('user_phone', 11)->comment('用户手机号');
            $table->string('user_email', 11)->comment('用户邮箱');

            $table->timestamps();
            $table->rememberToken();
            $table->engine = 'InnoDB';
            $table->charset = 'utf8';
            $table->collation = 'utf8_unicode_ci';
        });
    }
运行迁移文件命令报错
php artisan migrate
Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_intro'")
问题所在
 $table->tinyInteger('user_sex',false,false)->default(1)->comment('用户性别:0=>保密,1=>男,2=>女');
查看文档得知 int类型 自带 UNSIGNED 主键 特性
$table->bigIncrements('id');	等同于自增 UNSIGNED BIGINT(主键)列
$table->increments('id');	等同于自增 UNSIGNED INTEGER (主键)类型列
查看源码 int类型 第二个参数不是长度
/**
     * Create a new integer (4-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Database\Schema\ColumnDefinition
     */
    public function integer($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
    }
将int类型第二第三参数设为 false
然后出现新问题
Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")
需要指定 主键
$table->integer('user_id')->autoIncrement();

你可能感兴趣的:(PHP)