I have been trying to make simple queries with two conditions with start and end datetimes.
In development mode I don’t have any problem and it seems to work well (SQLite)
In production mode however I have the following error:
ActionView::Template::Error (PG::Error: ERROR: syntax error at or near "end"
LINE 1: ...ions" WHERE (start >= '2013-01-30T10:12:24Z' AND end >= '20...
: SELECT COUNT(*) FROM "missions" WHERE (start >= '2013-01-30T10:12:24Z' AND end >= '2013-01-30T10:12:24Z')):
^
6: <li class="mission clearfix">
1: <h4 class="heading-sep"><%= t(".upcoming_missions")%></h4>
2: <div class="missions-timeline">
3: <ul class="mission-list">
app/views/users/show.html.erb:12:in `_app_views_users_show_html_erb___1717297229253226868_63256380'
7: <div class="mission-wrap clearfix">
4: <% if @upcoming_missions.any? %>
5: <% @upcoming_missions.each do |mission| %>
app/views/users/_mission_timeline.html.erb:4:in `_app_views_users__mission_timeline_html_erb__2786253018340202740_62779040'
In my controller I have the following queries:
@upcoming_missions = Mission.where("start >= ? AND end >= ?", Time.now, Time.now).order("created_at DESC")
@current_missions = Mission.where("start <= ? AND end >= ?", Time.now, Time.now).order("created_at DESC")
@past_missions = Mission.where("start <= ? AND end <= ?", Time.now, Time.now).order("created_at DESC")
I tried other suggestions by including .utc in case PG reads time differently than SQLite… but I am not sure where the problem is.
Thank you for your help
Aurelien
The solution to the problem is that the word
endis a reserved keyword for PostgreSQL. Thank you Micapam for your answers and link.In my case, my Missions table had datetime columns named
startandend. When running with SQLite in development mode, it was not returning any errors and was sorting my missions the right way. Strangely enough,endis also a reserved keyword in SQLite but was still letting me run my queries.In order to solve the problem, I simply renamed my columns in a migration
It is good to check out these reserved keywords to make sure there are no conflicts.