Laravel schema builder INT (11) for integer () and INT (10) for unsignedInteger ()

Why does Laravel create an integer column as INT (11) and an unsigned integer column as INT (10)?

$table->integer('integer'); // INT(11)
$table->unsignedInteger('unsignedInteger'); // INT(10) unsigned
$table->integer('integer_then_unsigned')->unsigned(); // INT(10) unsigned

      

Since the maximum unsigned number can be almost twice as large, shouldn't it be the other way around?

+3


source to share


3 answers


Because of the minus sign when an integer can be signed.

Unsigned integers will be 10 digits, so its display length is 10.



Signed integers will require a space for the minus sign if negative. Therefore, apart from ten digits, you need something else for the sign.

+7


source


This is because the minus sign is in case your column is signed, which allows the field to have negative integers. You can have up to ten digits when your field is unsigned.



0


source


I have the same problem and I tried to add unsigned to id, but it didn’t help, but it still had int (10), this is a structure created by maggots migration:

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 outer constraints it will return an error:

alter table `product_user add constraint user_foreign 
foreign key (`user_id`) references `users` (`id`) on delete cascade;

      

eneral error: 1215 Unable to add foreign key constraint

My workaround is to remove unsigned from product_id, user_id

-1


source







All Articles