M
M
Mimuss2018-03-14 07:55:50
PostgreSQL
Mimuss, 2018-03-14 07:55:50

Why is 3 nil returned in the query?

I use PostgreSQL

2.5.0 :003 > Book.joins("INNER JOIN grants ON grants.book_id = books.id").where('grants.date' => Date.new(2000,1,1)..Date.today).select("grants.date as grants_date")
  Book Load (1.9ms)  SELECT  grants.date as grants_date FROM "books" INNER JOIN grants ON grants.book_id = books.id WHERE ("grants"."date" BETWEEN $1 AND $2) LIMIT $3  
 => #<ActiveRecord::Relation [#<Book id: nil>, #<Book id: nil>, #<Book id: nil>]>

The data itself looks like this: Book
2.5.0 :013 > Book
 => Book(id: integer, name: string, cipher: string, created_at: datetime, updated_at: datetime, library_id: integer, author: string, publisher: string, published_date: date, price: float, release_date: date)

Grant:
2.5.0 :014 > Grant
 => Grant(id: integer, date: datetime, book_id: integer, subscriber_id: integer, created_at: datetime, updated_at: datetime, date_of_return: date)

Books table
spoiler
Table "public.books"
     Column     |            Type             |                     Modifiers                      
----------------+-----------------------------+----------------------------------------------------
 id             | bigint                      | not null default nextval('books_id_seq'::regclass)
 name           | character varying(2000)     | not null
 cipher         | character varying(100)      | not null
 created_at     | timestamp without time zone | not null
 updated_at     | timestamp without time zone | not null
 library_id     | bigint                      | 
 author         | character varying(50)       | 
 publisher      | character varying(50)       | 
 published_date | date                        | 
 price          | double precision            | 
 release_date   | date                        | 
Indexes:
    "books_pkey" PRIMARY KEY, btree (id)
    "index_books_on_library_id" btree (library_id)
Foreign-key constraints:
    "fk_rails_9bb3dacd9b" FOREIGN KEY (library_id) REFERENCES libraries(id)
Referenced by:
    TABLE "grants" CONSTRAINT "fk_rails_76e4d1cfb5" FOREIGN KEY (book_id) REFERENCES books(id)

Grants table
spoiler
Table "public.grants"
     Column     |            Type             |                      Modifiers                      
----------------+-----------------------------+-----------------------------------------------------
 id             | bigint                      | not null default nextval('grants_id_seq'::regclass)
 date           | timestamp without time zone | not null
 book_id        | bigint                      | 
 subscriber_id  | bigint                      | 
 created_at     | timestamp without time zone | not null
 updated_at     | timestamp without time zone | not null
 date_of_return | date                        | 
Indexes:
    "grants_pkey" PRIMARY KEY, btree (id)
    "index_grants_on_book_id" btree (book_id)
    "index_grants_on_subscriber_id" btree (subscriber_id)
Foreign-key constraints:
    "fk_rails_75dda46977" FOREIGN KEY (subscriber_id) REFERENCES subscribers(id)
    "fk_rails_76e4d1cfb5" FOREIGN KEY (book_id) REFERENCES books(id)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2018-03-14
@Dem1

Because you didn't include id in select

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question