Framework: Timestamps are not prefixed when updating many-to-many relationship

Created on 11 Jan 2016  路  2Comments  路  Source: laravel/framework

I have a many-to-many relationship of Users and Notices set up in Eloquent. The pivot table has timestamps and a 'read' field (so each user can mark a notice as read independently). Here are the relevant model definitions:

class Notice extends Model
{
    public function users()
    {
        return $this->belongsToMany('App\Entities\User', 'notice_user_map')
            ->withPivot('read')
            ->withTimestamps();
    }
}

class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    public function notices()
    {
        return $this->belongsToMany('App\Entities\Notice', 'notice_user_map')
            ->withPivot('read')
            ->withTimestamps();
    }
}

However, when I try to mark all notices for a user as read I get an error. The code I'm using is

$user->notices()->wherePivot('read', 0)->update(['read' => 1]);

The error is

Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous (SQL: update notices inner join notice_user_map on notices.id = notice_user_map.notice_id set read = 1, updated_at = 2016-01-06 23:49:45 where notice_user_map.user_id = 2904708423 and notice_user_map.read = 0)

The columns that are set should have the table prefix, i.e. notice_user_map.read and notice_user_map.updated_at

Also the date isn't quoted, not sure if that's the exact query that was trying to be run or if that's just how Laravel is displaying it.

Laravel 5.1, PHP 5.5, MySQL 5.6

bug

Most helpful comment

I still have this issue
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous
Even i am not many-to-many relation

All 2 comments

Have the same problem , using a hasManyThrough relationship.

Also mentioned here but marked as closed: #5440 .

Laravel 5.2.29, PHP 5.5, MySQL 5.6

I still have this issue
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous
Even i am not many-to-many relation

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PheRum picture PheRum  路  112Comments

itsgoingd picture itsgoingd  路  81Comments

Demers94 picture Demers94  路  88Comments

mianshargeel picture mianshargeel  路  59Comments

robjbrain picture robjbrain  路  64Comments