ambiguous column name

Hi. I’m new to Yii and this is my first post. Issue is I am receiving “ambiguous column name errors”. I have three models which are:


  • article_id

  • title

  • fields[n]


  • taglink_id

  • tag_id

  • article_id


  • tag_id

  • title

I have a relation defined in article as:

'tags' => array(self::MANY_MANY, 'tag', 'taglink(article_id,tag_id)'),

I would like to retrieve all articles with a designated tag and in my controller am using:

$criteria->addCondition('tag_id = '.$tagId);


The issue is I am receiving error "ambiguous column name: tag_id". Additionally, the query in the log applies a COUNT instead of select (*) on the article such as:

SELECT COUNT(DISTINCT 't'."article_id") FROM 'article' 't'  LEFT OUTER JOIN 'taglink' 'tags_tags' ON ('t'."article_id"='tags_tags'."article_id") LEFT OUTER JOIN 'tag' 'tags' ON ('tags'."tag_id"='tags_tags'."tag_id")  WHERE (tag_id = 3)

When I prefix the criteria condition with the relation name as

$criteria->addCondition('tags.tag_id = '.$tagId);

, the select renders the fields of article instead of COUNT, but I receive error "no such column: tags.tag_id". Also, the SQL in the application.log does not apply the relationship such as:

SELECT 't'."article_id" AS "t0_c0", 't'."title" AS "t0_c1", 't'."body" AS "t0_c2", 't'."category_id" AS "t0_c3", 't'."license_type_id" AS "t0_c4", 't'."web_url" AS "t0_c5", 't'."is_active" AS "t0_c6", 't'."is_approved" AS "t0_c7", 't'."is_user_submission" AS "t0_c8", 't'."is_featured" AS "t0_c9", 't'."page_url" AS "t0_c10", 't'."created_on" AS "t0_c11", 't'."modified_on" AS "t0_c12" FROM 'article' 't'  WHERE (tags.tag_id = 3) LIMIT 10

Can anyone point me in the right direction on this query? Also, can anyone tell me why it is adding the "LIMIT 10" on the query?

Thank you

maybe must be flipped??


$criteria->addCondition('tags.tag_id = '.$tagId);

or maybe you must use $criteria->join();

This should work


$criteria->together = true;

$criteria->condition('tags.tag_id = '.$tagId);

The LIMIT clause is generated by pagination.


The additional line of “$criteria->together = true;” made it work. For reference, the order of the “$criteria” didn’t seem to make any difference.

Thank you so much!