Laravel 架构构建器 INT(11) on integer() 和 INT(10) on unsignedInteger()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25527642/
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 schema builder INT(11) on integer() and INT(10) on unsignedInteger()
提问by Artur K?pp
Why does Laravel create the integer column as INT(11) and unsigned integer column as INT(10)?
为什么 Laravel 将整数列创建为 INT(11),将无符号整数列创建为 INT(10)?
$table->integer('integer'); // INT(11)
$table->unsignedInteger('unsignedInteger'); // INT(10) unsigned
$table->integer('integer_then_unsigned')->unsigned(); // INT(10) unsigned
Since the unsigned integers maximum value can be almost twice as large, shouldn't it rather be the other way around?
由于无符号整数最大值几乎可以是两倍大,难道不应该相反吗?
回答by N.B.
Because of the minus sign when integer can be signed.
因为整数可以被签名时的减号。
Unsigned integers will have 10 digits, and its display length is therefore - 10.
无符号整数将有 10 位数字,因此其显示长度为 - 10。
Signed integers will require a space for the minus sign, if it's negative. Therefore, on top of 10 digits, you need another one for the sign.
如果是负数,有符号整数将需要一个空格作为减号。因此,在 10 位数字之上,您需要另一个作为符号。
回答by spetsnaz
It is because the minus sign in case your column is signed, which allows the field to have negative integers. You can have up to 10 digits when your field is unsigned.
这是因为减号以防您的列有符号,这允许该字段具有负整数。当您的字段未签名时,您最多可以有 10 位数字。
回答by Zoran
I have the same problem and I tried adding unsigned to id it did not help it still was int(10) this is structure that larval migration created:
我有同样的问题,我尝试将 unsigned 添加到 id 它没有帮助它仍然是 int(10) 这是幼虫迁移创建的结构:
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `product_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `product_user_unique` (`product_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
if you try to add foreign constrains it will return error:
如果您尝试添加外部约束,它将返回错误:
alter table `product_user add constraint user_foreign
foreign key (`user_id`) references `users` (`id`) on delete cascade;
eneral error: 1215 Cannot add foreign key constraint
一般错误:1215 无法添加外键约束
My workaround is to drop unsigned from product_id, user_id
我的解决方法是从 product_id、user_id 中删除 unsigned