Laravel迁移:定义为整数的字段将被视为自动增量?

时间:2016-06-08 12:52:20

标签: laravel

这是我的代码:

public function up()
    {
        Schema::create('sysmods', function (Blueprint $table) {
            $table->increments('mod_id');
            $table->string('mod_name','60');
            $table->string('mod_alias_name','60');
            $table->integer('mod_tb_id','6');// this will become auto increment?
            $table->timestamps();
        });
    }

当我运行migrate命令时,它说: “[PDOException]  SQLSTATE [42000]:语法错误或访问冲突:1075表def不正确  从头;只能有一个自动列,必须将其定义为键“

然后当我检查sql时,我发现了这个:      (SQL:create table la_sysmodsmod_id int unsigned not null auto_increm  ent主键,mod_name varchar(60)not null,mod_alias_name varchar(  60)not null, mod_tb_id int not null auto_increment primary key created _at timestamp null,updated_at timestamp null)默认字符集UTF  8整理utf8_unicode_ci)

标记为整数的字段'mod_tb_id'已成为增量。有什么问题?谢谢!

2 个答案:

答案 0 :(得分:3)

整数方法的第二个参数是autoIncrement变量。你需要删除它。

Laravel方法

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

改变这个:

$table->integer('mod_tb_id','6');// this will become auto increment?

对此:

$table->integer('mod_tb_id');

答案 1 :(得分:1)

您需要更换

$table->integer('mod_tb_id','6');// this will become auto increment?

$table->integer('mod_tb_id')->length(6);

$table->integer('mod_tb_id', false, false)->length(6);

integer()方法的第二个参数是autoincrement的标志,第三个参数是unsigned

希望有所帮助