neurony / laravel-duplicate

Duplicate any Eloquent model along with its underlying relationships
MIT License
122 stars 27 forks source link

It throws error when using $withCount = ['relation'] on child models #13

Closed mehrancodes closed 4 years ago

mehrancodes commented 4 years ago

I have a $withCount = ['downloads'] in my child model, and when I try to replicate the parent model, it tries to replicate the child model by adding an extra column to the model named downloads_count. here's the error

message": "SQLSTATE[HY000]: General error: 1 table resources has no column named downloads_count (SQL: insert into "resources" ("lesson_id", "type", "title", "url", "deleted_at", "course_id", "downloads_count") values (1, external_resource, Illo aut nisi vitae ut a aut laborum ut., https://www.youtube.com/watch?v=bTqVqk7FSmY, ?, 2, 0))

I have fixed this by adding this line to my duplicateOptions

    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance()
            ->excludeRelationColumns(['resources' => ['downloads_count']]);
    }
mehrancodes commented 4 years ago

I have submitted this issue on the Laravel framework repo since it throws this error when using the replicate model as well.

https://github.com/laravel/framework/issues/34192