Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot delete custom MorphPivot in Laravel 6.18.26 #33647

Closed
lukadriel7 opened this issue Jul 25, 2020 · 5 comments
Closed

Cannot delete custom MorphPivot in Laravel 6.18.26 #33647

lukadriel7 opened this issue Jul 25, 2020 · 5 comments

Comments

@lukadriel7
Copy link
Contributor

  • Laravel Version: 6.18.26
  • PHP Version: 7.4.8
  • Database Driver & Version: MariaDB 10.3.23

Description:

Trying to delete a custom MorphPivot Model using uuid as primary key result in an error:

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: delete from `comment` where (`` = 01418755-c68e-4ea5-8043-cef348c47445))'

Laravel uses an empty string instead of the id column to try and find the model.
Creating, updating and finding model by id still works.

Model Code:

<?php

namespace App;

use App\Traits\HasUuid;
use Illuminate\Database\Eloquent\Relations\MorphPivot;

class Comment extends MorphPivot
{
    use HasUuid;

    protected $casts = [
        'id' => 'string'
    ];

    protected $fillable = [
        'id',
        'parent_id',
        'user_id',
        'commentable_id',
        'commentable_type',
        'content',
        'created_at',
        'updated_at'
    ];

    public $incrementing = false;
    public $keyType = 'string';
    protected $primaryKey = 'id';
}

Migration Code:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comment', function (Blueprint $table) {
            $table->uuid('id');
            $table->primary('id');
            $table->text('content')->default("");
            $table->boolean('spoiler')->default(false);
            $table->boolean('hidden')->default(false);
            $table->boolean('edited')->default(false);
            $table->uuid('parent_id')->nullable();
            $table->uuid('user_id')->default((string) Illuminate\Support\Str::uuid());
            $table->uuid('commentable_id')->default((string) Illuminate\Support\Str::uuid());
            $table->string('commentable_type')->default(App\Post::class);
            $table->json('meta')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comment');
    }
}

Steps To Reproduce:

  • Create a laravel 6 project
  • Create a custom pivot model and its migration similarly to code posted above
  • Create a new record
  • Try deleting the record
@driesvints
Copy link
Member

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to report back and I'll gladly help you out and re-open this issue.

Thanks!

@lukadriel7
Copy link
Contributor Author

lukadriel7 commented Jul 25, 2020

@driesvints Hey, I did a quick check in the source code of MorphMany and found the problem. This condition is missing in the delete method of MorphPivot in Laravel 6.x tag source code.

if (isset($this->attributes[$this->getKeyName()])) {
   return (int) parent::delete();
}

the same instruction exist in the simple Pivot model through the AsPivot trait:
AsPivot trait laravel 6.x

MorphPivot Laravel 6.x

MorphPivot Laravel 7.x

Can you please check it ?

@lukadriel7
Copy link
Contributor Author

@driesvints Hey, I did a quick check in the source code of MorphMany and found the problem. This condition is missing in the delete method of MorphPivot in Laravel 6.x tag source code.

if (isset($this->attributes[$this->getKeyName()])) {
   return (int) parent::delete();
}

the same instruction exist in the simple Pivot model through the AsPivot trait:
AsPivot trait laravel 6.x

MorphPivot Laravel 6.x

MorphPivot Laravel 7.x

Can you please check it ?

@driesvints i can confirm that it is the problem, i change the laravel source code of MorphPivot in my project (I know it is bad but I just wanted to check) And it works.

@driesvints
Copy link
Member

@lukadriel7 feel free to attempt a pr to see if Taylor will merge it.

@lukadriel7
Copy link
Contributor Author

@driesvints , about that I don't really know how to write tests...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants