Skip to content

ActiveRecord reads id as null if table has no primary key #24857

@MOZGIII

Description

@MOZGIII

Steps to reproduce

  1. Create a rails app with Rails 4.2 with PostgreSQL database.
  2. Configure a PostgreSQL database and enable uuid-ossp.
  3. Create a books table with id being a UUID. Create some records manually.
  4. rails g model book
  5. In rails c.
[vagrant@localhost vagrant]$ rails c
Loading development environment (Rails 4.2.6)
[1] pry(main)> b = Book.first
  Book Load (13.2ms)  SELECT  "books".* FROM "books" LIMIT 1
=> #<Book:0x007fd5a13d4068
 id: nil,
 ...>
[2] pry(main)> b.attributes
=> {"id"=>"bfaae356-3f40-7ffc-30cc-bf5d9c95ffab",
...}
[3] pry(main)> 

Expected behavior

b.id to be equal to bfaae356-3f40-7ffc-30cc-bf5d9c95ffab

Actual behavior

b.id is null
But attributes['id'] has the correct value.

System configuration

Rails version:
4.2.6 (also happens with 5.0.0.beta4)
Ruby version:
2.3.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions