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

[PGSQL] multiple assignments to the same column #26995

Closed
SoGooDFR opened this issue Dec 29, 2018 · 10 comments
Closed

[PGSQL] multiple assignments to the same column #26995

SoGooDFR opened this issue Dec 29, 2018 · 10 comments

Comments

@SoGooDFR
Copy link

  • Laravel Version: 5.8-master
  • PHP Version: 7.3-fpm
  • Database Driver & Version: pgsql - Cockroachdb 2.1.3

Description:

When i update data in laravel 5.8, it will give me this:
array:3 [▼ "user.updated_at" => "2018-12-29 16:02:23" "last_cache_date" => "2018-12-29T16:02:23+00:00" "updated_at" => "2018-12-29 16:02:23" ]
And this cause this error:

SQLSTATE[XX000]: Internal error: 7 ERROR: multiple assignments to the same column "updated_at"

On laravel 5.7 no problem:
array:2 [▼ "last_cache_date" => "2018-12-29T16:08:06+00:00" "updated_at" => "2018-12-29 16:08:06" ]

Steps To Reproduce:

Upgrade to laravel 5.8. Update data.

@staudenmeir
Copy link
Contributor

Please post the whole query.

@SoGooDFR
Copy link
Author

SQLSTATE[XX000]: Internal error: 7 ERROR: multiple assignments to the same column "updated_at" (SQL: update "user" set "updated_at" = 2018-12-29 16:45:21, "last_cache_date" = 2018-12-29T16:45:21+00:00, "updated_at" = 2018-12-29 16:45:21 where "id" = 394853277715628033)

@staudenmeir
Copy link
Contributor

How are you generating the query? [...]->update([...])

@SoGooDFR
Copy link
Author

SoGooDFR commented Dec 29, 2018

In my model, i use timestamps = true.
This query is autogenerated by laravel from:

Illuminate\Database\Query\Builder (line 2641)
function update(array $values).

To get the results, i just put a dump($values).

@staudenmeir
Copy link
Contributor

You must be doing something to trigger the query.

Are you updating the model (e.g. $user->update([...]);)?

@SoGooDFR
Copy link
Author

This is my code (in a middleware):

$user = User::find(394853277715628033);
$user->permission_cache = json_encode($this->permissionCache);
$user->last_cache_date = new \DateTime();
$user->save();

@staudenmeir
Copy link
Contributor

What does your User model look like?

@SoGooDFR
Copy link
Author

<?php
namespace App\Model\User;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Ramsey\Uuid\Uuid;

/**
 * User model
 */
class User extends Authenticatable
{
    /**
     * @var string
     */
    protected $table = 'user';

    /**
     * @var array
     */
    protected $casts = [
        'search_key' => 'array'
    ];

    /**
     * @var array
     */
    private $join = [];

    public static function boot()
    {
        parent::boot();

        self::creating(function ($model) {
            $model->uuid = Uuid::uuid1()->toString();
        });
    }
}

@SoGooDFR
Copy link
Author

Debug stack trace on 5.8:
The function save() of Illuminate\Database\Eloquent\Model call performUpdate() function.
Illuminate\Database\Eloquent\Model::performUpdate() auto update the timestamps detected from the model because public $timestamps = true on User.

The method Illuminate\Database\Eloquent\Concerns\HasAttributes::getDirty() is calling and return this attributes:

array:2 [▼
 "last_cache_date" => DateTime @1546124401
 "updated_at" => "2018-12-29 23:00:01"
]

After, Illuminate\Database\Eloquent\Builder::update() is calling with addUpdatedAtColumn() into the same class. The method addUpdatedAtColumn() use qualifyColumn and return this attribute:

user.updated_at

So, addUpdatedAtColumn() finaly return:

array:3 [▼
 "user.updated_at" => "2018-12-29 23:08:55"
 "last_cache_date" => DateTime @1546124935
 "updated_at" => "2018-12-29 23:00:01"
]

@staudenmeir
Copy link
Contributor

Has been fixed.

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

3 participants