Project

General

Profile

Actions

Defect #42622

closed

Joining both atom_token and api_token on the User model causes an error due to the ambiguous column name "action"

Added by Lorin Thwaits 6 days ago. Updated 3 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Database
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

When using JOINs with the User model, if both atom_token and api_token are utilized like this:

User.joins(:atom_token, :api_token)

then this error results:

#<ActiveRecord::StatementInvalid:"PG::AmbiguousColumn: ERROR:  column reference \"action\" is ambiguous\nLINE 1: ... \"api_tokens_users\".\"user_id\" = \"users\".\"id\" AND (action='ap...\n

This can be fixed by prepending the proper table name in the `where` clause on these lines, 92 and 93 in user.rb:

  has_one :atom_token, lambda {where "#{table.name}.action='feeds'"}, :class_name => 'Token'
  has_one :api_token, lambda {where "#{table.name}.action='api'"}, :class_name => 'Token'

(Note that all that is being added here is #{table.name}.)

When doing this then the resulting SQL turns into:

SELECT "users".* FROM "users" INNER JOIN "tokens" ON "tokens"."user_id" = "users"."id" AND (tokens.action='feeds') INNER JOIN "tokens" "api_tokens_users" ON "api_tokens_users"."user_id" = "users"."id" AND (api_tokens_users.action='api') WHERE "users"."type" = $1

I have no idea how to create a pull request with SVN to provide this fix, so if someone could do this then awesome :)


Files

42622.patch (906 Bytes) 42622.patch Go MAEDA, 2025-04-24 12:30
Actions #1

Updated by Go MAEDA 5 days ago

  • File 42622.patch 42622.patch added
  • Subject changed from "PG::AmbiguousColumn: ERROR: column reference \"action\" is ambiguous" to Joining both atom_token and api_token on the User model causes an error due to the ambiguous column name "action"
  • Status changed from New to Confirmed
  • Target version set to 5.1.9

Thank you for reporting the issue and suggesting the fix.
I am setting the target version to 5.1.9.

Actions #2

Updated by Marius BĂLTEANU 5 days ago

  • Description updated (diff)
Actions #3

Updated by Marius BĂLTEANU 5 days ago

  • Description updated (diff)
Actions #4

Updated by Marius BĂLTEANU 5 days ago

Lorin Thwaits wrote:

I have no idea how to create a pull request with SVN to provide this fix, so if someone could do this then awesome :)

Unfortunately, you can't create a pull request, you need to post it as a patch / diff.

Actions #5

Updated by Go MAEDA 5 days ago

  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch in r23712.
Thank you for reporting and fixing the issue.

Actions #6

Updated by Go MAEDA 3 days ago

  • Status changed from Resolved to Closed

Merged the fix into the stable branches in r23720 and r23721.

Actions

Also available in: Atom PDF