在 Db 中创建的 Laravel 迁移布尔字段就像小整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44028862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:59:28  来源:igfitidea点击:

Laravel migration boolean field created in Db like tiny integer

phplaravellaravel-migrations

提问by Stefano Maglione

I wrote a migration in Laravel:

我在 Laravel 中写了一个迁移:

 Schema::create('test', function (Blueprint $table) {
        //
        $table->increments('id');
        $table->string('city','30')->unique();
        $table->string('car','30')->unique();
        $table->boolean('required');
        $table->string('street','100')->nullable();
        $table->json('files');
        $table->timestamp('created_at');
    });

the field required is defined as boolean but in the db (MySql) is created as tinyint. How is it possible?

所需的字段定义为布尔值,但在 db (MySql) 中创建为 tinyint。这怎么可能?

回答by Rezrazi

Tinyint is the same as boolean. Tinyintis an integer of size equal to 1 octet. When creating the column set as booleanthe the db creates it as a tinyint with a size of 1 bit. Thus making it's possible values 0and 1which is a boolean.

Tinyint 与boolean. Tinyint是大小等于 1 个八位字节的整数。创建列设置为booleandb 时,将其创建为大小为1 bit. 从而使其成为可能的值01哪个是boolean.



From MySQL documentation

来自 MySQL 文档

BOOL, BOOLEAN

These types are synonyms for TINYINT(1). A value of zerois considered false. Nonzero values are considered true

这些类型是 的同义词TINYINT(1)。的值zero被考虑false。考虑非零值true

Numeric Type Overview

数字类型概述