【问题标题】:Laravel Migration incorrect table definitionLaravel 迁移不正确的表定义
【发布时间】:2015-05-07 20:40:23
【问题描述】:

尝试运行以下迁移时:

    Schema::create('languages', function(Blueprint $table){

        $table->increments('id');

        $table->string('lang', 10);

        $table->string('name', 50);

        $table->integer('active', 2);

        $table->timestamps();
    });

我收到以下错误:

there can be only one auto column and it must be defined as a key

但 laravel 的文档指出:$table->increments('id'); Incrementing ID to the table (primary key)

知道如何处理吗? 提前致谢!

【问题讨论】:

    标签: php mysql laravel laravel-4


    【解决方案1】:

    问题是$table->integer('active', 2)

    这是integer的方法签名:

    public function integer($column, $autoIncrement = false, $unsigned = false)
    

    第二个参数实际上是一个自动递增的标志。 2 的计算结果为 true

    您不能指定整数的长度。但是,您可以改用tinyInteger

    $table->tinyInteger('active');
    

    【讨论】:

      【解决方案2】:

      你的问题是

      $table->integer('active', 2);
      

      传递给整数的第二个参数是一个布尔值,指示该列是否应为自增列,2 的值将被视为布尔值true

      编辑

      如果您只想要一个 short 整数(例如布尔值),请使用

      $table->tinyinteger('active');
      

      $table->boolean('active');
      

      【讨论】:

      • 我的错,以为参数应该是长度!
      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 2017-04-12
      • 2020-02-02
      • 2021-04-06
      • 1970-01-01
      • 2017-01-29
      • 2021-02-28
      • 2019-10-05
      相关资源
      最近更新 更多