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

Invalid delete query when has morphmany count relation #22413

Closed
fractalzombie opened this issue Dec 13, 2017 · 29 comments
Closed

Invalid delete query when has morphmany count relation #22413

fractalzombie opened this issue Dec 13, 2017 · 29 comments

Comments

@fractalzombie
Copy link

  • Laravel Version: 5.5.24
  • PHP Version: 7.2, 7.1
  • Database Driver & Version: PostgreSQL 9.5 & PostgreSQL 9.6

Description:

When we have morph to many relationship, we can't delete model, because it is generate invalid sql (screens are included) and delete is fails. Instead model id it gives model_type (class) from relation (??? why ???).

Steps To Reproduce:

Create model and relation model, then make relation morphMany to relation model, and then add to $withCount this relation and try to delete model.

Images:

Relation: https://ibb.co/nvDJvR
WithCount: https://ibb.co/fMwtUm
Invalid Query: https://ibb.co/e5OU26
Valid Query: https://ibb.co/gAcBFR

@sisve
Copy link
Contributor

sisve commented Dec 13, 2017

Could you provide the code snippets as text/code in your issue instead of embedding them as images on an external site? It would make them easier to read, copy/paste, and searchable in the future.

@fractalzombie
Copy link
Author

fractalzombie commented Dec 13, 2017

<?php

namespace App\Models;

use App\Traits\Filterable;
use App\Traits\Editable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;

/**
 * App\Models\Supplier
 */
class Supplier extends Model implements HasMedia
{
    use Editable, Filterable, HasMediaTrait, SoftDeletes;

    /**
     * @const string active status
     */
    public const ACTIVE = 'active';

    /**
     * @const string active status
     */
    public const INACTIVE = 'inactive';

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [
        'id',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'status',
        'post_code',
        'country',
        'region',
        'city',
        'address',
        'phone',
        'address',
        'fax',
        'website',
        'stripe_id'
    ];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = [
        'logo',
        'categories',
        'edited'
    ];

    /**
     * The relationship counts that should be eager loaded on every query.
     *
     * @var array
     */
    protected $withCount = [
        'products'
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'deleted_at'
    ];

    /**
     * The "booting" method of the model.
     *
     * @return void
     * @throws \InvalidArgumentException
     */
    protected static function boot(): void
    {
        parent::boot();

//        static::addGlobalScope(new SuppliersListingByRoleScope);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function logo(): MorphOne
    {
        return $this->morphOne(Media::class, 'model')
            ->where('collection_name', '=', Media::LOGO)
            ->latest();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function products(): MorphMany
    {
        return $this->morphMany(Product::class, 'owner');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function brands(): MorphToMany
    {
        return $this->morphToMany(Brand::class, 'brandable');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function users(): MorphMany
    {
        return $this->morphMany(User::class, 'institution');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function administrators(): MorphMany
    {
        $administrator = Role::administrator()->first();

        return $this->users()->whereHas('role', function (Builder $query) use ($administrator) {
            return $query->where('role_id', '=', $administrator->id);
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function discounts(): MorphMany
    {
        return $this->morphMany(Discount::class, 'discountable');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function categories(): BelongsToMany
    {
        return $this->morphToMany(FoodCategory::class, 'categorizable', 'categorizables');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function branches(): HasMany
    {
        return $this->hasMany(Branch::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function subscriptions(): MorphToMany
    {
        return $this->morphToMany(
            Subscription::class,
            'subscriptionable',
            'subscriptionables'
        )->withPivot('expire_at', 'icon', 'status');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function orders(): MorphMany
    {
        return $this->morphMany(Order::class, 'institution');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function favoritedByEstablishments(): MorphMany
    {
        return $this->morphMany(Favorite::class, 'who');
    }

    /**
     * @param Builder $builder
     * @param int $food_category_id
     * @param null|string $order
     *
     * @return Builder
     */
    public function scopeWhereSuppliedProductCategory(Builder $builder, int $food_category_id, ?string $order = 'ASC'): Builder
    {
        return $builder->whereHas('categories', function ($query) use ($food_category_id) {
            $query->where('food_category_id', $food_category_id);
        })->with('branches')->orderBy('name', $order);
    }
}

@Miguel-Serejo
Copy link

When reporting a bug, please try to post a minimal test case that reproduces your issue instead of your entire project's code.
There's a lot of noise in the code you posted that makes it hard to know what we should be looking at.

If I understand correctly, this would be the minimal code for an affected model:

class Supplier extends Model
{
  /**
  * The relationship counts that should be eager loaded on every query.
  * @var array
  */
  protected $withCount = [
    'products'
  ];

  /**
  * @return \Illuminate\Database\Eloquent\Relations\MorphMany
  */
  public function products(): MorphMany
  {
    return $this->morphMany(Product::class, 'owner');
  }
}

Now, does the error happen when you try to delete a product through the supplier or a supplier through a product?

@fractalzombie
Copy link
Author

When delete supplier. /api/v1/suppliers/:id

@Miguel-Serejo
Copy link

That's just your route. What is the actual code that you're using to delete your model?
Just following the steps you described initially, I did a reduced test case and I managed to delete the model just fine.

I still don't know if you're trying to report a bug in the framework or asking for help with your own code.

@fractalzombie
Copy link
Author

fractalzombie commented Dec 14, 2017

function (Supplier $supplier) { $supplier->delete(); }

Simple

@Miguel-Serejo
Copy link

In that case, I am unable to reproduce.

>>> $country->refresh()
=> App\Country {#1032
     id: 2,
     name: "test",
     created_at: "2017-11-28 13:34:26",
     updated_at: "2017-11-28 13:34:26",
     deleted_at: null,
     holidays_count: 1,
     holidays: Illuminate\Database\Eloquent\Collection {#1034
       all: [
         App\Holiday {#1038
           id: 358,
           date: "2222-02-02",
           description: "test",
           target_type: "national",
           target_id: 2,
           created_at: "2017-12-14 09:51:08",
           updated_at: "2017-12-14 09:51:08",
           deleted_at: null,
         },
       ],
     },
   }
>>> $country->delete();
=> true

Could you explain what happens when you attempt to delete the supplier? Do any exceptions get thrown?

@fractalzombie
Copy link
Author

fractalzombie commented Dec 14, 2017


{
    "message": "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (SQL: update \"suppliers\" set \"deleted_at\" = 2017-12-14 10:14:30, \"updated_at\" = 2017-12-14 10:14:30 where \"id\" = App\\Models\\Supplier)",
    "exception": "Illuminate\\Database\\QueryException",
    "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
    "line": 664,
    "trace": [
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
            "line": 624,
            "function": "runQueryCallback",
            "class": "Illuminate\\Database\\Connection",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
            "line": 490,
            "function": "run",
            "class": "Illuminate\\Database\\Connection",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
            "line": 423,
            "function": "affectingStatement",
            "class": "Illuminate\\Database\\Connection",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
            "line": 2149,
            "function": "update",
            "class": "Illuminate\\Database\\Connection",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
            "line": 778,
            "function": "update",
            "class": "Illuminate\\Database\\Query\\Builder",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Eloquent/SoftDeletes.php",
            "line": 77,
            "function": "update",
            "class": "Illuminate\\Database\\Eloquent\\Builder",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Eloquent/SoftDeletes.php",
            "line": 53,
            "function": "runSoftDelete",
            "class": "App\\Models\\Supplier",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
            "line": 770,
            "function": "performDeleteOnModel",
            "class": "App\\Models\\Supplier",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Support/HigherOrderTapProxy.php",
            "line": 34,
            "function": "delete",
            "class": "Illuminate\\Database\\Eloquent\\Model",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/app/Services/SupplierService.php",
            "line": 129,
            "function": "__call",
            "class": "Illuminate\\Support\\HigherOrderTapProxy",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/app/Http/Controllers/Rest/SupplierController.php",
            "line": 357,
            "function": "destroy",
            "class": "App\\Services\\SupplierService",
            "type": "->"
        },
        {
            "function": "destroy",
            "class": "App\\Http\\Controllers\\Rest\\SupplierController",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
            "line": 54,
            "function": "call_user_func_array"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
            "line": 45,
            "function": "callAction",
            "class": "Illuminate\\Routing\\Controller",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 212,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\ControllerDispatcher",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 169,
            "function": "runController",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 658,
            "function": "run",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/tymon/jwt-auth/src/Http/Middleware/Authenticate.php",
            "line": 32,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Tymon\\JWTAuth\\Http\\Middleware\\Authenticate",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
            "line": 41,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 102,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 660,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 635,
            "function": "runRouteWithinStack",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 601,
            "function": "runRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 590,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 176,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 56,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 46,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 102,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 151,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 116,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/vagrant/www/coster/public/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}

this one

@Miguel-Serejo
Copy link

That is quite odd. Are you overriding the delete(), getKey() or getKeyName() methods in any of your traits?

@fractalzombie
Copy link
Author

No, it's clear

@Miguel-Serejo
Copy link

I still can't reproduce this no matter what I try.

Please run the following commands in tinker and paste the results:

>>>$supplier = App\Models\Supplier::first();
>>>$supplier->delete();

@fractalzombie
Copy link
Author

fractalzombie commented Dec 14, 2017

vagrant@homestead:~/www/coster$ art tinker
Psy Shell v0.8.16 (PHP 7.2.0-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
$supplier = App\Models\Supplier::first();
=> App\Models\Supplier {#1343
     id: 1,
     name: "Gleichner, Marquardt and Donnelly",
     email: "shane.bergstrom@kuhn.biz",
     status: "active",
     post_code: "00094-6649",
     country: "SE",
     region: "Australia",
     city: "Boehmton",
     address: "ducimus",
     phone: "890-432-7273 x36579",
     fax: "(298) 892-2428 x6486",
     website: "http://simonis.com/voluptatem-voluptatem-quod-error-voluptates-deleniti-ab-eos",
     stripe_id: null,
     created_at: "2017-12-14 10:30:59",
     updated_at: "2017-12-14 10:30:59",
     deleted_at: null,
     products_count: 0,
     logo: null,
     categories: Illuminate\Database\Eloquent\Collection {#1341
       all: [],
     },
     edited: null,
   }
 $supplier->delete();
Illuminate\Database\QueryException with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (SQL: update "suppliers" set "deleted_at" = 2017-12-14 12:36:42, "updated_at" = 2017-12-14 12:36:42 where "id" = App\Models\Supplier)

@Miguel-Serejo
Copy link

I'm still unable to reproduce.

Does it work if you remove the $withCount property? What about if you remove the Editable/Filterable/HasMediaTrait` traits?

@fractalzombie
Copy link
Author

fractalzombie commented Dec 14, 2017

I remove traits and it's still not working, but when I remove withCount it's working, I said about it at report in first post.

@Shkeats
Copy link

Shkeats commented Dec 14, 2017

@36864 Is your test using softDeletes on the model? It looks like @fractalzombie is.

@Miguel-Serejo
Copy link

@Shkeats yes, as you can see in the example I posted where my model contained the deleted_at property.

What I'm not using is the HasMedia interface, but I can't use more time trying to replicate this right now.

@fractalzombie
Copy link
Author

@36864 @Shkeats I've tried without any traits, and problem still exists...

@mcbanderson
Copy link

I've experienced some strange behavior with 'withCount' in the past (#19388), both when using the withCount property and withCount method. These issues are possibly related. @fractalzombie downgrading to 5.3 solved the issue for me, so I'd be curious if it solves the issue you are experiencing as well. Not sure if it is feasible for you to try that though.

@fractalzombie
Copy link
Author

@mcbanderson Can't downgrade, because project starts with 5.5

@themsaid
Copy link
Member

Please upgrade your app to the latest 5.5 release and test, if it doesn't work please include some simple steps to reproduce, you don't have to share the entire code of your project classes, just the parts that matter, not as images, but in a way we can copy/paste and test on our local machines.

@fractalzombie
Copy link
Author

I deployed new project, and test again with $withCount and it's not working too. I include all simple steps already.

download new project, and create two models, one with new morphMany relation, and try to delete than model.

@yehuda4e
Copy link

yehuda4e commented Jan 7, 2018

I've got the same problem. Every model that I add the withCount property and it has a polymorphic relationship, can't be deleted.
With the same type error as yours..
My version is: 5.5.25

Steps To Reproduce:
Create 2 models.
User & Likes. User is simple model and likes is polymorphic, and they both connected with polymorphic relations.
Now add the withCount propery to the user model with the value of likes and try to delete it.

Update
If you put the withcount property to a query, fetch the results, and then delete it, it works fine.
The problem is only when the propery in the model itself..

@szwagros
Copy link

szwagros commented Mar 4, 2018

I've tested this with latest 5.4.* and 5.5.* version (cannot test 5.6 at the moment).

Steps to reproduce

Fresh Laravel install with
composer create-project --prefer-dist laravel/laravel withcount54 5.4.*
or
composer create-project --prefer-dist laravel/laravel withcount55 5.5.*

Two simple models: Article and Comment. Article with polymorphic relation and property $withCount

    protected $withCount = ['comments'];

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

Create article in DB and then:

  • on 5.4.36 after running update on article model e.g.:
$article->update(['body' => 'test']);

generates SQL error with following query

SQL: update "articles" set "body" = test, 
"updated_at" = 2018-03-04 09:58:54 where "id" = App\Article

also $article->delete() generates similar error:

SQL: delete from "articles" where "id" = App\Article
  • on 5.5
$article->update(['body' => 'test']);

works and returns true as expected.

$article->delete() generate error with following query

SQL: delete from "articles" where "id" = App\Article

@staudenmeir
Copy link
Contributor

This has been fixed in 5.6.

@sisve
Copy link
Contributor

sisve commented Jul 15, 2018

@staudenmeir Do you happen to know which PR/commit fixed it? Because they should probably be backported to 5.5 LTS too.

@staudenmeir
Copy link
Contributor

@sisve It has been fixed in #24240. I'll look into a backport.

@staudenmeir
Copy link
Contributor

This has been fixed in 5.5.41.

@staudenmeir
Copy link
Contributor

@laurencei Can be closed.

@fractalzombie
Copy link
Author

Thanks

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

No branches or pull requests

10 participants