【问题标题】:Laravel 5.5 set size of integer fields in migration fileLaravel 5.5 在迁移文件中设置整数字段的大小
【发布时间】:2018-06-07 20:20:15
【问题描述】:

我是 laravel 的新手。

现在我正在使用 migrate 命令做一个表,但是字段的长度不适用。 Laravel 不提供此选项。 ?

以下是我的代码:

$table->increments('id')->length(11);
$table->dateTime('created_time');
$table->integer('bank_id')->length(11);
$table->tinyInteger('is_black')->length(1);

字段is_black的长度应该是1,但实际上生成的是4。 我该如何解决这个问题?

任何建议或意见将不胜感激。

提前谢谢你

【问题讨论】:

  • 如我所见,您需要一个长度为 11 的整数。当您使用无符号整数时,这是一个常见的错误。非无符号整数是 11 个长度,无符号整数是 10 个长度。所以尝试将 unsigned 标志设置为 false,或删除 ->unsigned() 运算符。

标签: laravel laravel-5.5


【解决方案1】:

你不能这样做,但你可以使用不同类型的整数:

$table->bigInteger()
$table->mediumInteger()
$table->integer()
$table->smallInteger()
$table->tinyInteger()

https://laravel.com/docs/5.5/migrations#columns

【讨论】:

  • 我不能这样做?? .. 哦,我明白了。谢谢你的回答
【解决方案2】:

根据https://laravel.com/docs/5.5/migrations, 您可以使用以下类型之一:

$table->bigInteger('votes');
$table->integer('votes');

$table->mediumInteger('votes'); 
$table->smallInteger('votes');
$table->tinyInteger('votes');
$table->unsignedBigInteger('votes');
$table->unsignedMediumInteger('votes'); 
$table->unsignedSmallInteger('votes');  
$table->unsignedTinyInteger('votes');   

【讨论】:

  • 其实有5种。
  • @AlexeyMezenin 谢谢,你说得对,整数类型更多,实际上,在无符号版本中,有超过 5 种类型。
  • unsigned 只是integer()->unsigned() 的快捷方式,所以它不是类型。
  • 好的,但这怎么可能选择长度?
【解决方案3】:

根据https://laravel.com/docs/5.1/migrations,从 Laravel 5.1 开始,您可以使用 boolean 列类型来创建长度为 1 的“布尔型”TINYINT (MySQL)。所以,例如:

$table->boolean('nameOfColumn');

【讨论】:

    【解决方案4】:

    这对我来说是解决方案! 在函数内部运行。

        $tableName = 'tblresefeage';
        $comments = 'Resumen efectividad por agencia';
        Schema::create($tableName, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('agencia')->comment('Agencia');
            $table->date('fechacierre')->comment('Fecha cierre');           
            $table->timestamps();
        });
        DB::statement('ALTER TABLE tblresefeage MODIFY COLUMN agencia INTEGER (11);');
    
        Schema::table($tableName, function (Blueprint $table) {
            $table->foreign('agencia')->on('tblentage')->references('cveentage')->onDelete('cascade');
        });
    
        DB::statement("ALTER TABLE `$tableName` comment '".$comments."'");
    

    【讨论】:

      【解决方案5】:

      您可以使用这种方式。祝你好运。

      $table->decimal('is_black',1,0);

      【讨论】:

        【解决方案6】:

        此代码对我有用。

        $table->addColumn(
            'tinyInteger', 'field_name',
            [
                'length'   => 2,
                'default'  => '1',
                'autoIncrement' => false,
                'unsigned' => true,
                'comment'  => 'Some comments'
            ]
        );
        

        【讨论】:

        • 您能否通过代码块改进建议代码的格式?
        【解决方案7】:
        $table->increments('id',11);
        $table->dateTime('created_time');
        $table->integer('bank_id',11);
        $table->tinyInteger('is_black',1);
        

        【讨论】:

        • 来自评论:虽然这段代码可能会解决问题,但一个好的答案还应该解释代码的什么以及它如何解决问题。
        • 另外,这也行不通。您是否在文档中的某处阅读过可以传递第二个参数的内容?
        • 这段代码不工作,如果我放第二个参数,我得到这个错误: SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;只能有一个自动列,并且必须将其定义为键(SQL:创建表tvasidbigint unsigned not null auto_increment 主键,tauxint not null auto_increment 主键,created_attimestamp null , updated_at timestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')
        • integer 方法的第二个参数用于将 autoIncrement 标记为 true,而不是设置字段的长度。
        猜你喜欢
        • 2018-05-01
        • 1970-01-01
        • 2023-03-06
        • 2018-04-26
        • 2018-02-13
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        相关资源
        最近更新 更多