【问题标题】:Laravel migration: fields that defined as interger wil be treated as auto increment?Laravel 迁移:定义为整数的字段将被视为自动增量?
【发布时间】:2016-10-08 18:06:44
【问题描述】:

这是我的代码:

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 命令时,它说: " [PDO异常] SQLSTATE [42000]:语法错误或访问冲突:1075 表定义不正确 启动;只能有一个自动列,并且必须将其定义为键”

然后,当我检查 sql 时,我发现了这个: (SQL: 创建表la_sysmods (mod_id int unsigned not null auto_increm ent 主键,mod_namevarchar(60) 不为空,mod_alias_namevarchar( 60) not null, mod_tb_id int not null auto_increment 主键, created _at timestamp null, updated_at timestamp null) 默认字符集 utf 8 整理 utf8_unicode_ci)

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

【问题讨论】:

    标签: laravel


    【解决方案1】:

    整数方法的第二个参数是 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');
    

    【讨论】:

      【解决方案2】:

      你需要更换你的

      $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 的标志。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-24
        • 1970-01-01
        • 2017-12-07
        • 2022-11-01
        • 2017-05-11
        • 2016-03-15
        • 2022-10-25
        • 1970-01-01
        相关资源
        最近更新 更多