possible bug

19 views
Skip to first unread message

stuart yarus

unread,
Sep 3, 2014, 12:10:05 AM9/3/14
to rub...@googlegroups.com
Hi,

Looks like I have to be a member of Google Groups to post....

The issue is that a SELECT statement in a ruby method returns some but not all the fields requested, but the SELECT statement does return all the requested fields if pasted directly into a psql session.  The code lines are:

conn = PG::Connection.open( :dbname => 'music' )
conn.exec( 'SELECT p.piece_title, p.piece_number, p.album_id, p.performance,
      p.location, f.type, o.orch_name, t.first_name, t.last_name, x.first_name,      x.last_name
    FROM pieces p, form f, orchestras o, conductors t, composers x
    WHERE p.id = 9 AND p.music_form = f.id AND p.orchestra = o.id AND p.conductor = t.id AND p.composer = x.id' ) do |res|
      res.each do |row| p row.values
   end
end

The fields missing from the output are  'x.first_name' and 'x.last_name'.  The rest of the requested data is in the output.

Notice that columns in two of the tables each have names first_name and last_name.  ALTERing first_name and last_name columns in one table to   'first_name1' and 'last_name1'  produces the desired result using the (updated) ruby code.

Am I doing something stupid or wrong?  Should I upload a pg_dump of the data?

This happens in Fedora 19 in pg 0.14.1 and pg 0.17.1, using
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux] and
postgres (PostgreSQL) 9.2.8.

Thanks for working on the pg gem.  I like it a lot.

Stuart



Jeremy Evans

unread,
Sep 3, 2014, 12:57:34 AM9/3/14
to rub...@googlegroups.com
row.values returns a hash where the keys are the column aliases you used in the query.  If you don't use unique column aliases, some of the columns you selected are not going to appear in the hash.  So I think this behavior is expected.  Just alias the columns in the query, no need to alter the table definition:

x.first_name AS first_name1,      x.last_name AS last_name1

Thanks,
Jeremy

stuart yarus

unread,
Sep 3, 2014, 1:28:31 AM9/3/14
to rub...@googlegroups.com


Seems reasonable ... and it works, too.

Thank you,

Stuart
Reply all
Reply to author
Forward
0 new messages