kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.44k stars 270 forks source link

Multiple Primary Keys defined #160

Closed PimMeulenkamp closed 1 year ago

PimMeulenkamp commented 1 year ago

Describe the bug migration is generated with an $table->integer('xx', true) // autoIncrement which laravel sees as a primary key. if the table also consists of constraint primary key it will throw an error because there is already an primary key set.

To Reproduce Steps to reproduce the behavior:

  1. Create table '...'
  2. add a integer column with autoincrement
  3. add $table->primary(['columname]);
  4. Run php artisan migrate:generate ...
  5. See error

Expected behavior remove autoincrement so there is not a primary key before the constraint

Screenshots

afbeelding afbeelding

Details (please complete the following information):

kitloong commented 1 year ago

Hi @Pimmeul Thanks for reporting.

I attempted to fix this previously but didn't have a good solution then. Let me look into this again.

kitloong commented 1 year ago

Hi @Pimmeul

Following is the way for the feature to work:

Schema::create('table', function (Blueprint $table) {
    $table->integer('id', true); // Will create primary key
    $table->integer('field1');
    $table->integer('field2');

    $table->dropPrimary(); // Drop previous key

    $table->primary(['field1', 'field2']); // Recreate primary key
});

Laravel migration will create a primary key for you if you define autoincrement.

May I know in what use case you need a autoincrement column without a primary key?

If you have an autoincrement column you should use it as your primary key.

Then create a unique constraint for the other columns.

$table->unique(['field1', 'field2']);
kitloong commented 1 year ago

Will close this issue for now