Error: Integrity constraint violation: 1052 Column
Asked Answered
E

3

6

why I'm getting this?

Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'favorites' in field list is ambiguous

SQL Query: UPDATE `twitter`.`tractions` AS `Traction` LEFT JOIN `twitter`.`profiles` AS `Profile` ON (`Traction`.`profile_id` = `Profile`.`id`) SET `Traction`.`favorites` = `favorites` + 1, `Traction`.`errors` = `errors` + 0 WHERE `Traction`.`profile_id` = 4 AND `Traction`.`date` = '2013-01-11'

CakePHP code:

$this->Traction->updateAll(array(
                "Traction.favorites"=>"`favorites` + $favorites",
                "Traction.errors"=>"`errors` + $errors"
                ), array(
                'Traction.profile_id'=>$profile['Profile']['id'],
                'Traction.date'=>date('Y-m-d')
            ));

-- Table structure for table tractions

CREATE TABLE IF NOT EXISTS `tractions` (
  `id` int(10) NOT NULL auto_increment,
  `date` date default NULL,
  `profile_id` int(10) default NULL,
  `followings` int(10) default '0',
  `unfollowings` int(10) default '0',
  `favorites` int(10) default '0',
  `retweets` int(10) default '0',
  `thanks` int(10) default '0',
  `errors` int(10) default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
Einkorn answered 11/1, 2013 at 15:36 Comment(0)
T
21

you need to specify the tableName, since multiple tables contains the same column name,

"Traction.favorites"=>"Traction.`favorites` + $favorites"
Tiannatiara answered 11/1, 2013 at 15:38 Comment(0)
I
3

Apparently both of the tables contain the column favorites, might also contain the column errors

As you are joining a second table you probably want to set Traction.favorites = Profile.favorites + $favorites and Traction.errors = Profile.errors + $errors

As said by JW. you need to use a full identifier

Illomened answered 11/1, 2013 at 15:47 Comment(0)
N
0

if u Use in WhereHas

Example

enter code here  $courses=Course::where('status',1)
        ->with('categories')
        ->whereHas('categories',function ($query)use ($request){
            return $query->where('categories.id',$request->categories_id);
       })->get();

if u have filter Course by Categoriy_id

use 'relationname'=> for example categories u must call categories.id instead of id

Numeral answered 31/7, 2022 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.