blueprint icon indicating copy to clipboard operation
blueprint copied to clipboard

Generation of multiple Primary Keys / Auto Increment

Open mahfoudfx opened this issue 3 years ago • 2 comments
trafficstars

  • Laravel Version: 9.29.0
  • PHP Version: 8.10.0
  • Blueprint Version: 2.4.0
  • Platform: Mac | Windows | Linux

Issue:

When using size attribute with any integer blueprint integer (bigInteger, mediumInteger, tinyInteger, smallInteger, etc...), it will generate a Laravel migration format that call SQL to create a Primary Key with Auto Increment. Please check this similar generated Issue.

draft.yaml:

code_no: smallInteger:3 unsigned unique

will generate

$table->unsignedSmallInteger('code_no', 3)->unique()

It maybe more adequate to generate

$table->unsignedSmallInteger('code_no')->length(3)->unique()

mahfoudfx avatar Sep 12 '22 16:09 mahfoudfx

Is $table->unsignedSmallInteger('code_no', 3)->unique() incorrect?

jasonmccreary avatar Sep 14 '22 11:09 jasonmccreary

As stated in the link of my previous post:

if you use any integer blueprint (bigInteger, mediumInteger, tinyInteger, smallInteger, etc...) with any second parameter (other than 0) you are telling Laravel to make an integer with an auto_increment property, this will return: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")

So this syntax is Ok for string but not for integers. I have to admit that when I first investigated the error, I started with my draft file, which was Ok, than I checked migration file, the generated code were as expected, than I have dig deeper and learn that is from Laravel itself.

mahfoudfx avatar Sep 14 '22 16:09 mahfoudfx

Gotcha. Feel free to submit a PR with the fix. Otherwise, I will patch this in a future live stream.

jasonmccreary avatar Oct 06 '22 19:10 jasonmccreary

@mahfoudfx

There is no such thing as integer length. https://github.com/laravel/framework/issues/1465#issuecomment-18700321

ghostwriter avatar Dec 08 '22 02:12 ghostwriter

@jasonmccreary can this be closed based on https://github.com/laravel/framework/issues/1465#issuecomment-18700321?

faustbrian avatar May 06 '23 02:05 faustbrian

I think the "fix" here is to ignore any length for all integer columns. Blueprint should not generate code which adds a length parameter or chains ->length() to any integer column.

jasonmccreary avatar May 06 '23 11:05 jasonmccreary