{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/33742","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/33742/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/33742/comments","events_url":"https://api.github.com/repos/rails/rails/issues/33742/events","html_url":"https://github.com/rails/rails/issues/33742","id":354896939,"node_id":"MDU6SXNzdWUzNTQ4OTY5Mzk=","number":33742,"title":"Arrays are read incorrectly from Postgres when previously written to using index number","user":{"login":"joallard","id":949482,"node_id":"MDQ6VXNlcjk0OTQ4Mg==","avatar_url":"https://avatars0.githubusercontent.com/u/949482?v=4","gravatar_id":"","url":"https://api.github.com/users/joallard","html_url":"https://github.com/joallard","followers_url":"https://api.github.com/users/joallard/followers","following_url":"https://api.github.com/users/joallard/following{/other_user}","gists_url":"https://api.github.com/users/joallard/gists{/gist_id}","starred_url":"https://api.github.com/users/joallard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joallard/subscriptions","organizations_url":"https://api.github.com/users/joallard/orgs","repos_url":"https://api.github.com/users/joallard/repos","events_url":"https://api.github.com/users/joallard/events{/privacy}","received_events_url":"https://api.github.com/users/joallard/received_events","type":"User","site_admin":false},"labels":[{"id":70310659,"node_id":"MDU6TGFiZWw3MDMxMDY1OQ==","url":"https://api.github.com/repos/rails/rails/labels/PostgreSQL","name":"PostgreSQL","color":"fbca04","default":false},{"id":107191,"node_id":"MDU6TGFiZWwxMDcxOTE=","url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-28T20:45:36Z","updated_at":"2018-08-28T23:59:38Z","closed_at":null,"author_association":"NONE","body":"I'm trying to read back an integer array column after I have written it, in SQL, using the index number.\r\n\r\nIf table `users` has a column `t.integer :numbers, array: true`, and in SQL, on a row, I set `SET numbers[3] = 10`, then on reading it back in SQL:\r\n\r\n```sql\r\nbug-array-reading=# select * from users;\r\n id |  numbers\r\n----+------------\r\n  1 | [3:3]={10}\r\n(1 row)\r\n\r\nbug-array-reading=# select numbers[3] from users;\r\n numbers\r\n---------\r\n      10\r\n```\r\n\r\nThere will be this special array notation. (Presumably not to have to fill a lot of elements with null spaces)\r\n\r\nWhen read in Activerecord, this will not be retained. This means that if I use this array as a numerical associative array, the read will not be correct.\r\n\r\n### Reproduction script\r\nThis script assumes a postgres db `bug-array-reading`, with the default auth options.\r\n\r\n```rb\r\nbegin\r\n  require \"bundler/inline\"\r\nrescue LoadError => e\r\n  $stderr.puts \"Bundler version 1.10 or later is required. Please update your Bundler\"\r\n  raise e\r\nend\r\n\r\ngemfile(false) do\r\n  source 'https://rubygems.org'\r\n  gem 'rails', github: \"rails/rails\"\r\n  gem 'pg'\r\n  gem 'pry'\r\n  gem 'pry-rescue'\r\nend\r\n\r\nrequire 'active_record'\r\nrequire 'minitest/autorun'\r\n\r\nActiveRecord::Base.establish_connection(\r\n  adapter: 'postgresql',\r\n  database: 'bug-array-reading',\r\n)\r\nActiveRecord::Base.logger = Logger.new(STDOUT)\r\n\r\nclass User < ActiveRecord::Base\r\nend\r\n\r\nActiveRecord::Schema.define do\r\n  create_table :users do |t|\r\n    t.integer :numbers, array: true\r\n  end unless table_exists?(:users)\r\nend\r\n\r\n\r\ndescribe \"Array reading\" do\r\n  before do\r\n    u = User.create!\r\n\r\n    User.connection.execute(\"UPDATE users SET numbers[3] = 10 WHERE id = #{u.id}\")\r\n    u.reload\r\n    @u = u\r\n  end\r\n\r\n  attr_reader :u\r\n\r\n  # SELECT numbers[3] FROM users WHERE id = #{u.id}\r\n  # would give 10\r\n\r\n  it \"dumps array\" do\r\n    assert_equal([nil, nil, nil, 10], u.numbers)\r\n  end\r\n\r\n  it \"reads index at value\" do\r\n    assert_equal(10, u.numbers[3])\r\n  end\r\nend\r\n```\r\n\r\n### System configuration\r\n**Rails version**: master\r\n\r\n**Ruby version**: 2.5.1\r\n\r\nUsing Postgres 9.6.1\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/416778548","html_url":"https://github.com/rails/rails/issues/33742#issuecomment-416778548","issue_url":"https://api.github.com/repos/rails/rails/issues/33742","id":416778548,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNjc3ODU0OA==","user":{"login":"matthewd","id":1034,"node_id":"MDQ6VXNlcjEwMzQ=","avatar_url":"https://avatars1.githubusercontent.com/u/1034?v=4","gravatar_id":"","url":"https://api.github.com/users/matthewd","html_url":"https://github.com/matthewd","followers_url":"https://api.github.com/users/matthewd/followers","following_url":"https://api.github.com/users/matthewd/following{/other_user}","gists_url":"https://api.github.com/users/matthewd/gists{/gist_id}","starred_url":"https://api.github.com/users/matthewd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthewd/subscriptions","organizations_url":"https://api.github.com/users/matthewd/orgs","repos_url":"https://api.github.com/users/matthewd/repos","events_url":"https://api.github.com/users/matthewd/events{/privacy}","received_events_url":"https://api.github.com/users/matthewd/received_events","type":"User","site_admin":false},"created_at":"2018-08-28T23:59:38Z","updated_at":"2018-08-28T23:59:38Z","author_association":"MEMBER","body":"SQL arrays are 1-indexed while Ruby arrays are 0-indexed, so I think the assertions should be:\r\n\r\n```ruby\r\n  it \"dumps array\" do\r\n    assert_equal([nil, nil, 10], u.numbers)\r\n  end\r\n\r\n  it \"reads index at value\" do\r\n    assert_equal(10, u.numbers[2])\r\n  end\r\n```"}}
{"actor":{"display_login":"matthewd"}}