I have a one to many relationship between two tables (User name: string group: integer) and Relationship (source: integer, target: integer,value: integer). They are related so that userid=relatioship.source.
I am trying to get some json out of it so I have written:
def self.including_relationships
User.joins("INNER JOIN relationships ON users.id = relationships.source").select("users.name, users.group, relationships.source, relationships.target, relationships.value").each_with_object(Hash.new{|h, k| h[k] = []}) do |a, obj|
obj['nodes'] << a.slice('name','group')
obj['links'] << a.slice('source', 'target', 'value')
However I get the error
SQLite3::SQLException: near "group": syntax error: SELECT users.name, users.group, relationships.source, relationships.target, relationships.value FROM "users" INNER JOIN relationships ON users.id = relationships.source
when I remove users.group and 'group' in the slice, it works fine.
I have checked my database schema which looks as follows:
ActiveRecord::Schema.define(version: 20150603200530) do
create_table "relationships", force: true do |t|
t.integer "source"
t.integer "target"
t.integer "value"
end
create_table "users", force: true do |t|
t.string "name"
t.integer "group"
end
end
The tables are all populated as they should be. I can't figure out what the exact syntax error is.
Group
is a reserved keyword and you should not use it as your column name.Basic misconception is people will think
Group By
collectively as a reserved one.Group
andBy
are two different reserved keywords.Change the column name
group
to something else. It will work. Or If you want to use reserved keyword, based on the documentationPlease refer the list of reserved keywords for sqlite for more information.