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

A small issue in postgresql_adapter.rb (quote_column_name) #52

Closed
ged opened this issue Nov 19, 2010 · 1 comment
Closed

A small issue in postgresql_adapter.rb (quote_column_name) #52

ged opened this issue Nov 19, 2010 · 1 comment

Comments

@ged
Copy link
Owner

ged commented Nov 19, 2010

Original report by crazycrv (Bitbucket: crazycrv, ).


This is regarding a small issue I observed in postgresql adapter for rails. I am using postgre 9.0 as database with rails 2.3.8 and pg adapter 0.9.0 x86-mswin32.

I have roles and users models in my application and they both are associated with each other through a bridge table roles_users.

In roles_user.rb I have specified composite primary key as "set_primary_keys :user_id, :role_id" ( I am using composite_primary_keys (2.3.2) gem )

Now when I call "self.roles.push(Role.find(:first, :conditions => {:name => 'user'}))" it gives me following error

Column "user_id,role_id" does not exist.
INSERT INTO "roles_users" ("updated_at", "created_at", "user_id", "role_id") VALUES (NULL, NULL, 16, 5) RETURNING "user_id,role_id"

After looking into it I found that the error is because of following method of postgrsql_adapter.rb

#!ruby

def quote_column_name(name) #:nodoc:
       PGconn.quote_ident(name.to_s)
end

On executing the roles.push query this method is being passed following input by insert method in the same file

Input: [:user_id, :role_id]

The method calls quote_indent and generates following output which causes PG Error mentioned above.

Output: ""user_id,role_id""

To solve this I have changed the method as given below and it worked fine.

#!ruby

def quote_column_name(name) #:nodoc:

        if name.is_a(Array)

          quoted_columns = []

          for column_name in name

            quoted_columns << PGconn.quote_ident(column_name.to_s)

          end

          quoted_columns.join(',')

        else

          PGconn.quote_ident(name.to_s)

        end
end

With this new code the output generated for the same input is ""user_id","role_id""

I am not sure whether its a bug in the postgresql_adapter.rb file or I am doing some silly mistake.

@ged
Copy link
Owner Author

ged commented Nov 22, 2010

Original comment by Michael Granger (Bitbucket: ged, GitHub: ged).


The PostgreSQL adapter for ActiveRecord isn't part of this library, it's part of ActiveRecord. I suggest you search for the issue in [[https://rails.lighthouseapp.com/projects/8994-ruby-on-rails|their bugtracker]], and if it hasn't been mentioned yet, submit it there. Thanks!

@ged ged closed this as completed Nov 22, 2010
@ged ged added this to the Pending milestone Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant