{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/27514","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/27514/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/27514/comments","events_url":"https://api.github.com/repos/rails/rails/issues/27514/events","html_url":"https://github.com/rails/rails/issues/27514","id":198185888,"number":27514,"title":"Precision option for arrays of timestamps are ignored on save.","user":{"login":"Nerian","id":259568,"avatar_url":"https://avatars.githubusercontent.com/u/259568?v=3","gravatar_id":"","url":"https://api.github.com/users/Nerian","html_url":"https://github.com/Nerian","followers_url":"https://api.github.com/users/Nerian/followers","following_url":"https://api.github.com/users/Nerian/following{/other_user}","gists_url":"https://api.github.com/users/Nerian/gists{/gist_id}","starred_url":"https://api.github.com/users/Nerian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nerian/subscriptions","organizations_url":"https://api.github.com/users/Nerian/orgs","repos_url":"https://api.github.com/users/Nerian/repos","events_url":"https://api.github.com/users/Nerian/events{/privacy}","received_events_url":"https://api.github.com/users/Nerian/received_events","type":"User","site_admin":false},"labels":[{"id":107191,"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1","default":false},{"id":128692,"url":"https://api.github.com/repos/rails/rails/labels/needs%20feedback","name":"needs feedback","color":"ededed","default":false},{"id":70310659,"url":"https://api.github.com/repos/rails/rails/labels/PostgreSQL","name":"PostgreSQL","color":"fbca04","default":false},{"id":107186,"url":"https://api.github.com/repos/rails/rails/labels/regression","name":"regression","color":"e10c02","default":false},{"id":126910270,"url":"https://api.github.com/repos/rails/rails/labels/With%20reproduction%20steps","name":"With reproduction steps","color":"009800","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-12-30T19:31:17Z","updated_at":"2016-12-31T17:18:06Z","closed_at":null,"body":"I am using ruby 2.3.3,  Rails 5.0.1 with PostgreSQL 9.6.1. I want to have an array of timestamps with a precision level of 6.\r\n\r\nGiven these migrations:\r\n\r\n```\r\ncreate_table :contracts do |t|\r\n  t.datetime :start_date, null: false, precision: 6\r\n  t.datetime :invoiced_dates, array: true, default: [], precision: 6\r\nend\r\n```\r\n\r\nI would expect this to happen:\r\n\r\n```\r\nc = Contract.new\r\n\r\nc.start_date = Time.current\r\nc.invoiced_dates << Time.current\r\nc.save\r\n\r\nc.start_date.usec\r\n  => something\r\n\r\nc.invoiced_dates.first.usec\r\n  => something\r\n```\r\n\r\nInstead, the datetimes stored in the array are stored with a precision value of 0. No milliseconds stored. The regular timestamp (datetime) column works fine.\r\n\r\n```\r\nc.start_date.usec\r\n  => something\r\n\r\nc.invoiced_dates.first.usec\r\n  => 0\r\n```\r\n\r\nThe table schema\r\n\r\n```\r\nrails_test=# \\d+ contracts;\r\nTable \"public.contracts\"\r\n      Column       |               Type               |                       Modifiers                        | Storage  | Stats target | Description \r\n-------------------+----------------------------------+--------------------------------------------------------+----------+--------------+-------------\r\n id                | integer                          | not null default nextval('contracts_id_seq'::regclass) | plain    |              | \r\n next_invoice_date | timestamp(6) without time zone   | not null                                               | plain    |              | \r\n invoiced_dates    | timestamp(6) without time zone[] | default '{}'::timestamp without time zone[]            | extended |              | \r\n created_at        | timestamp without time zone      | not null                                               | plain    |              | \r\n updated_at        | timestamp without time zone      | not null                                               | plain    |              | \r\nIndexes:\r\n    \"contracts_pkey\" PRIMARY KEY, btree (id)\r\nHas OIDs: no\r\n```\r\n\r\nSo looks like the migration is correct.\r\n\r\nAfter using Rails to create a Contract, this is how the DB looks like:\r\n\r\n```\r\nrails_test=# select * from contracts;\r\n id |     next_invoice_date      |     invoiced_dates      |         created_at         |         updated_at         \r\n----+----------------------------+-------------------------+----------------------------+----------------------------\r\n  1 | 2016-12-28 21:26:14.445997 | {\"2016-12-28 21:26:22\"} | 2016-12-28 21:26:22.604793 | 2016-12-28 21:26:22.604793\r\n(1 row)\r\n```\r\n\r\nAs you can see the precision is not being stored correctly.\r\n\r\nIf we update the value manually:\r\n\r\n```\r\nrails_test=# UPDATE contracts set invoiced_dates = '{\"2016-12-28 21:26:22.123456\"}' where id = 1;                                                             \r\nUPDATE 1\r\nrails_test=# select * from contracts;                                                                                                                          id |     next_invoice_date      |         invoiced_dates         |         created_at         |         updated_at         \r\n----+----------------------------+--------------------------------+----------------------------+----------------------------\r\n  1 | 2016-12-28 21:26:14.445997 | {\"2016-12-28 21:26:22.123456\"} | 2016-12-28 21:26:22.604793 | 2016-12-28 21:26:22.604793\r\n(1 row)\r\n```\r\n\r\nIf then we use Rails's console and take a look at the value:\r\n\r\n```\r\n2.4.0-rc1 :007 > c = Contract.first\r\n  Contract Load (3.5ms)  SELECT  \"contracts\".* FROM \"contracts\" ORDER BY \"contracts\".\"id\" ASC LIMIT $1  [[\"LIMIT\", 1]]\r\n => #<Contract id: 1, next_invoice_date: \"2016-12-28 21:26:14\", invoiced_dates: [Wed, 28 Dec 2016 21:26:22 UTC +00:00], created_at: \"2016-12-28 21:26:22\", updated_at: \"2016-12-28 21:26:22\"> \r\n2.4.0-rc1 :008 > c.invoiced_dates\r\n => [Wed, 28 Dec 2016 21:26:22 UTC +00:00] \r\n2.4.0-rc1 :009 > c.invoiced_dates.first\r\n => Wed, 28 Dec 2016 21:26:22 UTC +00:00 \r\n2.4.0-rc1 :010 > c.invoiced_dates.first.usec\r\n => 123456\r\n```\r\n\r\nAs you can see, it shows the microseconds. So it looks like there is a bug in Active Record when it saves the value."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/269874167","html_url":"https://github.com/rails/rails/issues/27514#issuecomment-269874167","issue_url":"https://api.github.com/repos/rails/rails/issues/27514","id":269874167,"user":{"login":"Nerian","id":259568,"avatar_url":"https://avatars.githubusercontent.com/u/259568?v=3","gravatar_id":"","url":"https://api.github.com/users/Nerian","html_url":"https://github.com/Nerian","followers_url":"https://api.github.com/users/Nerian/followers","following_url":"https://api.github.com/users/Nerian/following{/other_user}","gists_url":"https://api.github.com/users/Nerian/gists{/gist_id}","starred_url":"https://api.github.com/users/Nerian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nerian/subscriptions","organizations_url":"https://api.github.com/users/Nerian/orgs","repos_url":"https://api.github.com/users/Nerian/repos","events_url":"https://api.github.com/users/Nerian/events{/privacy}","received_events_url":"https://api.github.com/users/Nerian/received_events","type":"User","site_admin":false},"created_at":"2016-12-31T17:18:06Z","updated_at":"2016-12-31T17:18:06Z","body":"That's correct. I was missing a `bundle exec`."}}
{"actor":{"display_login":"Nerian"}}