Skip to content

Commit 7fec017

Browse files
author
Nathan Esayeas
authored
Fix: Invalid UUID relationships (#398)
1 parent 236a0e6 commit 7fec017

9 files changed

+113
-14
lines changed

src/Lexers/ModelLexer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ private function buildModel(string $name, array $columns)
184184
$model->addColumn($column);
185185

186186
$foreign = collect($column->modifiers())->filter(function ($modifier) {
187-
return (is_array($modifier) && key($modifier) === 'foreign') || $modifier === 'foreign';
187+
return collect($modifier)->containsStrict('foreign') || collect($modifier)->has('foreign');
188188
})->flatten()->first();
189189

190-
if ($column->name() !== 'id' && (in_array($column->dataType(), ['id', 'uuid']) || $foreign)) {
190+
if (($column->name() !== 'id') && ($column->dataType() === 'id') || $foreign) {
191191
$reference = $column->name();
192192

193193
if ($foreign && $foreign !== 'foreign') {

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ public function modelTreeDataProvider()
795795
['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
796796
['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
797797
['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'],
798+
['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'],
798799
['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
799800
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
800801
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ public function modelTreeDataProvider()
661661
['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements.php'],
662662
['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types.php'],
663663
['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships.php'],
664+
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'],
664665
];
665666
}
666667

@@ -687,6 +688,7 @@ public function laravel8ModelTreeDataProvider()
687688
['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements-laravel8.php'],
688689
['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types-laravel8.php'],
689690
['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships-laravel8.php'],
691+
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship-laravel8.php'],
690692
];
691693
}
692694

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
models:
2+
AgeCohort:
3+
id
4+
timestamps
5+
name: string:100
6+
description: string:500 nullable
7+
min_age: integer nullable
8+
max_age: integer nullable
9+
uuid: uuid unique
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateAgeCohortsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('age_cohorts', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name', 100);
19+
$table->string('description', 500)->nullable();
20+
$table->integer('min_age')->nullable();
21+
$table->integer('max_age')->nullable();
22+
$table->uuid('uuid')->unique();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('age_cohorts');
35+
}
36+
}

tests/fixtures/models/all-column-types-laravel8.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,4 @@ class AllType extends Model
9595
'unsignedSmallInteger' => 'integer',
9696
'unsignedTinyInteger' => 'integer',
9797
];
98-
99-
100-
public function uuid()
101-
{
102-
return $this->belongsTo(\App\Uuid::class);
103-
}
10498
}

tests/fixtures/models/all-column-types.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,4 @@ class AllType extends Model
100100
'timestamp',
101101
'timestampTz',
102102
];
103-
104-
105-
public function uuid()
106-
{
107-
return $this->belongsTo(\App\Uuid::class);
108-
}
109103
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class AgeCohort extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'name',
19+
'description',
20+
'min_age',
21+
'max_age',
22+
'uuid',
23+
];
24+
25+
/**
26+
* The attributes that should be cast to native types.
27+
*
28+
* @var array
29+
*/
30+
protected $casts = [
31+
'id' => 'integer',
32+
];
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class AgeCohort extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'name',
16+
'description',
17+
'min_age',
18+
'max_age',
19+
'uuid',
20+
];
21+
22+
/**
23+
* The attributes that should be cast to native types.
24+
*
25+
* @var array
26+
*/
27+
protected $casts = [
28+
'id' => 'integer',
29+
];
30+
}

0 commit comments

Comments
 (0)