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

updated_at in custom pivot table works incorrectly #34548

Closed
dendev21 opened this issue Sep 25, 2020 · 4 comments
Closed

updated_at in custom pivot table works incorrectly #34548

dendev21 opened this issue Sep 25, 2020 · 4 comments

Comments

@dendev21
Copy link

  • Laravel Version: 7.21.0
  • PHP Version: 7.4.8
  • Database Driver & Version: postgres:9.6

Description:

Defining UPDATED_AT as null in custom intermediate pivot table seems to be broken. $this->getUpdatedAtColumn() gets column name from parent model instead of custom pivot and as result it fails inserting to undefined column.
In version 5.8 column name was retrieved by static::UPDATED_AT and behaved right.

Illuminate\Database\Eloquent\Concerns\HasTimestamps:updateTimestamps()

protected function updateTimestamps()
{
    $time = $this->freshTimestamp();

    $updatedAtColumn = $this->getUpdatedAtColumn();

    if (! is_null($updatedAtColumn) && ! $this->isDirty($updatedAtColumn)) {
        $this->setUpdatedAt($time);
    }

    $createdAtColumn = $this->getCreatedAtColumn();

    if (! $this->exists && ! is_null($createdAtColumn) && ! $this->isDirty($createdAtColumn)) {
        $this->setCreatedAt($time);
    }
}
@driesvints
Copy link
Member

I'm suspecting #30697 to be the culprit but trying to find out why this is breaking for you. Can you please paste your models/pivot and how you're using this exactly?

@dendev21
Copy link
Author

I've created a demo project which shows how to reproduce the issue. You can see an exception executing php artisan demo:sync within app container

Illuminate\Database\QueryException 

SQLSTATE[42703]: Undefined column: 7 ERROR:  column "updated_at" of relation "post_has_comments" does not exist
LINE 1: update "post_has_comments" set "created_at" = $1, "updated_a...
                                                      ^ (SQL: update "post_has_comments" set "created_at" = 2020-09-27 19:46:15, "updated_at" = 2020-09-27 19:46:15 where "id" = 1)

Post model has relation to Comment model using PostHasComments pivot model.
Moreover table name in PostHasComments does not work at all. I had to add table name in comments relation

@driesvints
Copy link
Member

After a long and deep dive into this I've determined that this has never worked, not even in 5.8. I tested with your code in a fresh 5.8 install and I get the same error. The Pivot always expects a created_at and updated_at column to be present and indeed derives their names from the parent model if not set explicitly. Their names are set on the pivot model through the pivotCreatedAt and pivotUpdatedAt properties rather than a UPDATED_AT constant. That's a bit of a bizar difference but it's the way it currently and afaik always has worked.

As this is sort of expected behavior (for now) I'm closing this. We can look into improving this but that's an enhancement rather. I'll send in a PR to the docs to document this. Thanks.

@driesvints
Copy link
Member

PR for docs was merged: laravel/docs#6427

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