laravel-shift/blueprint

Explicit foreign not explicit

Opened this issue · 1 comments

  • Laravel Version: 11.6.0
  • PHP Version: 8.2
  • Blueprint Version: 2.10
  • Platform: Mac | Windows | Linux

Issue:

Explicit foreign:<table.column> does not work. It tries to be smart when it should not be smart.

models:
  Offer:
    uuid: uuid
    text: text
    user_id: id foreign:user.id // explicit table and column

This generates this schema:

Schema::create('offers', function (Blueprint $table) {
    $table->id();
    $table->uuid('uuid');
    $table->text('text');
    $table->foreignId('user_id')->constrained(); // this is wrong, should be constrained('user', 'id')
    $table->timestamps();
});

This will try to create constrained foreign key to non-existing table users. It should create constrained foreign key to table user instead. Specifying protected $table = 'user'; in User.php has no effect on the behaviour.

Another issue:

models:
  Offer:
    uuid: uuid
    text: text
    user_id: id foreign:persons.id // explicit table and column

This generates this schema:

Schema::create('offers', function (Blueprint $table) {
    $table->id();
    $table->uuid('uuid');
    $table->text('text');
    $table->foreignId('user_id')->constrained('persons'); // this is wrong, should be constrained('persons', 'id');
    $table->timestamps();
});

While it is true that constrained('persons'); has the same effect on database as constrained('persons', 'id'); today - Shift should not deal with effects on database at all. It should act as translation layer between .yaml file and Laravel migration specs. One would want to set explicit column in migration files for various reasons. For example, there are migration text analyzers which do not deal with Laravel magic conventions, they deal with explicit options.

So, it tries to be smart when it should not be smart. It should be just dumb when explicit parameters are specified.

Thanks. Feel free to open a PR. Otherwise, I'll look into this in the coming weeks.