Skip to content

Commit 4c71e53

Browse files
authored
πŸ”₯ Do not return key field if there is no primary key (#85)
* Do not return key field if there is no primary key * Add tests for pivots without primary key
1 parent 533443e commit 4c71e53

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

β€Žsrc/Eloquent/ModelSchema.php

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ protected function getKeyField(): array
209209
{
210210
$key = $this->instance->getKeyName();
211211

212+
if (! $key) {
213+
return [];
214+
}
215+
212216
return [$key => $this->registry->field($this->registry->ID())->fillable(false)->unique()];
213217
}
214218

β€Žtests/Feature/AttachPivotMutationTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function it_lets_you_attach_pivot_ids_with_pivot_data()
4949
{ id: "'.$role->id.'", customPivot: { comment: "foobar" } }
5050
]) {
5151
id
52+
customRoles {
53+
customPivot {
54+
comment
55+
}
56+
}
5257
}
5358
}
5459
';

β€Žtests/Stubs/Models/UserRole.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class UserRole extends Pivot
88
{
9+
protected $primaryKey = null;
10+
911
public $fillable = [
1012
'comment',
1113
];

β€Žtests/migrations/0000_00_00_000005_create_role_user_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class CreateRoleUserTable extends Migration
1111
public function up()
1212
{
1313
Schema::create('role_user', function ($table) {
14-
$table->increments('id');
1514
$table->unsignedInteger('user_id');
1615
$table->unsignedInteger('role_id');
1716
$table->text('comment')->nullable();

0 commit comments

Comments
Β (0)