首页 文章

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

提问于
浏览
3

这是我的代码:

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表定义不正确;只能有一个自动列,必须将其定义为键”

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

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

2 回答

  • 3

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

    The Laravel method

    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

    你需要更换你的

    $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 .

    希望能帮助到你

相关问题