Artur K.
Artur K.

Reputation: 3309

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

Why does Laravel create the integer column as INT(11) and 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 unsigned integers maximum value can be almost twice as large, shouldn't it rather be the other way around?

Upvotes: 10

Views: 26543

Answers (3)

spetsnaz
spetsnaz

Reputation: 1291

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.

Upvotes: 1

N.B.
N.B.

Reputation: 14071

Because of the minus sign when integer can be signed.

Unsigned integers will have 10 digits, and its display length is therefore - 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.

Upvotes: 19

Zoran
Zoran

Reputation: 2783

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:

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

My workaround is to drop unsigned from product_id, user_id

Upvotes: -1

Related Questions