在 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
Laravel migration boolean field created in Db like tiny integer
提问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
. Tinyint
is an integer of size equal to 1 octet. When creating the column set as boolean
the the db creates it as a tinyint with a size of 1 bit
. Thus making it's possible values 0
and 1
which is a boolean
.
Tinyint 与boolean
. Tinyint
是大小等于 1 个八位字节的整数。创建列设置为boolean
db 时,将其创建为大小为1 bit
. 从而使其成为可能的值0
,1
哪个是boolean
.
From MySQL documentation
来自 MySQL 文档
BOOL, BOOLEAN
These types are synonyms for TINYINT(1)
. A value of zero
is considered false
. Nonzero values are considered true
这些类型是 的同义词TINYINT(1)
。的值zero
被考虑false
。考虑非零值true